-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa 11504 - Dominos.cc
89 lines (73 loc) · 1.52 KB
/
UVa 11504 - Dominos.cc
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
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int seen[N], vis[N], cnt;
stack <int> S;
vector <int> g[N], t[N];
set <int> scc;
void dfs(int u){
if(seen[u])
return;
seen[u] = 1;
for(int v: g[u]){
dfs(v);
}
S.push(u);
}
void get(int u){
if(vis[u])
return;
vis[u] = 1;
scc.insert(u);
for(int v: t[u]){
get(v);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T--){
int m, n;
cin >> n >> m;
for(int i = 0; i < N; i++){
seen[i] = 0;
vis[i] = 0;
t[i].clear();
g[i].clear();
}
while(m--){
int u, v;
cin >> u >> v;
g[u].push_back(v);
t[v].push_back(u);
}
for(int i = 1; i <= n; i++){
dfs(i);
}
int cur = 1;
unordered_map <int, bool> comp;
while(!S.empty()){
int u = S.top();
S.pop();
scc.clear();
get(u);
bool out = false;
for(int v: scc){
for(int k: t[v]){
if(!scc.count(k)){
out = true;
}
}
}
if(!scc.empty()) comp[cur++] = (out);
}
int ans = 0;
for(int i = 1; i < cur; i++){
ans += (!comp[i]);
}
cout << ans << endl;
}
return 0;
}