-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCC.cpp
69 lines (58 loc) · 1.11 KB
/
SCC.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
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define MAX 1000
vector< int > graph[MAX];
vector< int > rvrs[MAX];
int vis[MAX];
stack< int > finish;
int n,m;
void dfs(int src){
vis[src]=1;
int u=src;
for(int i=0;i<graph[u].size();i++){
int v=graph[u][i];
if(!vis[v]){
vis[v]=1;
dfs(v);
}
}
finish.push(u);
}
void dfs2(int src){
vis[src]=1;
int u=src;
cout<<u<<" ";
for(int i=0;i<rvrs[u].size();i++){
int v=rvrs[u][i];
if(!vis[v]){
dfs2(v);
}
}
}
void scc(){
memset(vis,0,sizeof vis);
for(int i=1;i<=n;i++)
if(!vis[i])
dfs(i);
memset(vis,0,sizeof vis);
while(!finish.empty()){
int i=finish.top();
finish.pop();
if(!vis[i]){
dfs2(i);
cout<<endl;
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
graph[u].pb(v);
rvrs[v].pb(u);
}
scc();
return 0;
}