-
Notifications
You must be signed in to change notification settings - Fork 1
/
UVA_11504.cpp
68 lines (62 loc) · 1.19 KB
/
UVA_11504.cpp
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
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define endl "\n"
#define newline cout << "\n"
#define puts(_content_) cout << _content_ << "\n"
using namespace std;
const int MAXV = 1E5+5;
int V, E;
vector<int> adj[MAXV];
vector<bool> visited;
stack<int> S;
void dfs (int u) {
visited[u] = true;
for(int v : adj[u]) {
if(visited[v]) continue;
dfs(v);
}
S.push(u);
}
void dfs2(int u) {
visited[u] = true;
for(int v : adj[u]) {
if(visited[v]) continue;
dfs2(v);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef Home
freopen ("input.txt", "r", stdin);
#endif
int tc;
cin >> tc;
while(tc--) {
cin >> V >> E;
int u, v;
REP(i, 0, V) adj[i].clear();
visited.assign(V, false);
REP(i, 0, E) {
cin >> u >> v;
adj[u-1].push_back(v-1);
}
REP(v, 0, V) {
if(!visited[v]) {
dfs(v);
}
}
int answer = 0;
visited.assign(V, false);
while(!S.empty()) {
int v = S.top(); S.pop();
if(visited[v]) continue;
++answer;
dfs2(v);
}
puts(answer);
}
return 0;
}