-
Notifications
You must be signed in to change notification settings - Fork 0
/
scc.cpp
59 lines (53 loc) · 1.07 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
#include <bits/stdc++.h>
using namespace std ;
#define int long long
vector<vector<int>> g , gcomp;
void dfs(int start , vector<int>& v , int* visited){
if(visited[start]) return ;
visited[start] = 1 ;
for(auto i : g[start]){
dfs(i,v,visited) ;
}
v.push_back(start) ;
}
void dfs2(int start , vector<int>& v , int* visited){
if(visited[start]) return ;
visited[start] = 1 ;
for(auto i : gcomp[start]){
dfs2(i,v,visited) ;
}
v.push_back(start) ;
}
int32_t main(){
int n ;
cin >> n ;
g.resize(n) ;
gcomp.resize(n) ;
for(int i = 0 ; i < n ; i++){
int f , s ;
cin >> f >> s;
f-- , s-- ;
g[f].push_back(s) ;
gcomp[s].push_back(f) ;
}
vector<int> v;
int visited[n] = {} ;
for(int i = 0 ; i < n ; i++){
if(!visited[i]) dfs(i,v,visited) ;
}
for(int i = 0 ; i < n ; i++) visited[i] = 0 ;
vector<vector<int>> comp ;
while(v.size()){
if(!visited[v.back()]){
vector<int> temp ;
dfs2(v.back() , temp , visited) ;
comp.push_back(temp) ;
}
v.pop_back() ;
}
for(auto i : comp){
for(auto j : i) cout << j << " " ;
cout << endl ;
}
return 0 ;
}