-
Notifications
You must be signed in to change notification settings - Fork 80
/
Kosaraju.cpp
79 lines (63 loc) · 1.08 KB
/
Kosaraju.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
69
70
71
72
73
74
75
76
77
78
79
#include<bits/stdc++.h>
#define REP(i,n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define endl '\n'
using namespace std;
vi ar[1001];
vi tr[1001];
vi order;
vi SCC;
int vis[1001];
void dfs(int node)
{
vis[node] = 1;
for(int child : ar[node])
if(vis[child] == 0)
dfs(child);
order.pb(node);
}
void dfs1(int node)
{
vis[node] = 1;
for(int child : tr[node])
if(vis[child] == 0)
dfs1(child);
SCC.pb(node);
}
int main()
{
int n , a , b , m , t;
cin>>t;
while(t--)
{
cin>>n>>m;
REP(i , n) ar[i].clear() , tr[i].clear() , vis[i] = 0;
order.clear();
REP(i , m)
{
cin>>a>>b;
ar[a].pb(b);
tr[b].pb(a);
}
REP(i , n)
if(vis[i] == 0) dfs(i);
REP(i , n) vis[i] = 0;
REP(i , n)
if(vis[order[n-i]] == 0)
{
SCC.clear();
dfs1(order[n - i]);
cout<<"dfs1() called from "<<order[n-i]<<" and printing SCC"<<endl;
for(int node : SCC)
cout<<node<<" ";
cout<<endl;
}
}
}