-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrongly connected component.cpp
More file actions
82 lines (77 loc) · 1.34 KB
/
strongly connected component.cpp
File metadata and controls
82 lines (77 loc) · 1.34 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
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
vector < int > G[N] , RG[N];
int time__ = 0;
int dtime__[N] , ftime__[N];
bool visited[N] , revisited[N];
vector < int > vv;
bool taken[N];
void dfs (int u) {
if (visited[u]) {
return ;
}
visited[u] = 1;
dtime__[u] = ++time__;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!visited[v]) {
dfs (v);
}
}
ftime__[u] = ++time__;
vv.push_back(u);
}
void dfs2 (int u) {
if (revisited[u]) {
return ;
}
revisited[u] = 1;
for (int i = 0; i < RG[u].size(); i++) {
int v = RG[u][i];
if (!revisited[v]) {
dfs2 (v);
}
}
}
void clean_up () {
memset (visited , 0 , sizeof (visited));
memset (revisited , 0 , sizeof (revisited));
vv.clear();
for (int i = 0; i < N; i++) {
G[i].clear ();
RG[i].clear ();
}
}
int main () {
ios_base::sync_with_stdio (false);
cin.tie();
cout.tie();
int T;
cin >> T;
while (T--) {
clean_up ();
int n; cin >> n;
int m; cin >> m;
for (int i = 0; i < m; i++) {
int x , y;
cin >> x >> y;
G[x].push_back (y);
RG[y].push_back (x);
}
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
dfs (i);
}
}
reverse (vv.begin() , vv.end());
int cnt = 0;
for (int i = 0; i < vv.size(); i++) {
if (!revisited[vv[i]]) {
dfs2 (vv[i]);
cnt++;
}
}
cout << cnt;
}
}