-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path043-book.cpp
More file actions
117 lines (116 loc) · 2.49 KB
/
Copy path043-book.cpp
File metadata and controls
117 lines (116 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Book [1572A]
* Problem: https://codeforces.com/problemset/problem/1572/A
* Verdict: ACCEPTED Solved: 2022-06-12
* Language: C++20 (GCC 11-64)
* Runtime: 140 ms Memory: 22300 KB
* Tags: binary search, brute force, data structures, dp, graphs, implementation, sortings
* Author: BidoTeima
* Source: https://codeforces.com/contest/1572/submission/160267907
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void ACPLS()
{
#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void moo(string fileName){
freopen((fileName+".in").c_str(),"r",stdin);
freopen((fileName+".out").c_str(),"w",stdout);
}
#define tc int tttt;cin>>tttt;while(tttt--)
vector<int>adj[(int)2e5+3],adj_rev[(int)2e5+3];
vector<int>vis;
bool invalid;
int dp[(int)2e5+3];
int dfs(int node){
if(vis[node]){
return dp[node];
}
vis[node]=1;
int ret = 0;
for(int child:adj[node]){
ret=max(ret,dfs(child)+(child>=node));
}
return dp[node]=ret;
}
vector<int>topo;
void dfs_topo(int node){
if(vis[node])return;
vis[node]=1;
for(int child:adj[node]){
dfs_topo(child);
}
topo.push_back(node);
}
vector<int>scc;
void dfs_scc(int node){
if(vis[node])return;
vis[node]=1;
scc.push_back(node);
for(int child:adj_rev[node]){
dfs_scc(child);
}
}
int main()
{
ACPLS();
tc{
topo.clear();
invalid=0;
int n;
cin>>n;
vis.assign(n+5,0);
for(int v = 1; v <= n; v++){
adj[v].clear();
adj_rev[v].clear();
}
for(int v = 1; v <= n; v++){
dp[v]=0;
vis[v]=0;
int k;
cin>>k;
for(int j = 0; j < k; j++){
int u;
cin>>u;
adj[v].push_back(u);
adj_rev[u].push_back(v);
}
}
vis.assign(n+5,false);
for(int i = 1; i <= n; i++){
if(!vis[i]){
dfs_topo(i);
}
}
vis.assign(n+5,false);
reverse(topo.begin(),topo.end());
for(int node:topo){
if(!vis[node]){
scc.clear();
dfs_scc(node);
if(scc.size()>1){
invalid=1;
break;
}
}
}
if(invalid){
cout<<"-1\n";
continue;
}
vis.assign(n+3,0);
int ans=0;
for(int i = 1; i <= n; i++){
if(!vis[i])ans=max(ans,dfs(i));
}
cout<<ans+1<<'\n';
}
}