-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCC.cpp
More file actions
64 lines (59 loc) · 1.22 KB
/
SCC.cpp
File metadata and controls
64 lines (59 loc) · 1.22 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
#include<bits/stdc++.h>
using namespace std;
vector <int> adj[100];
vector <int> rev_adj[100];
bool visited[100];
stack<int>st;
int nodes, edges, x, y,l=1;
void dfs(int s) {
visited[s] = true;
for(int i = 0;i < adj[s].size();++i){
if(visited[adj[s][i]] == false)
dfs(adj[s][i]);
}
st.push(s);
}
void rev_dfs(int s) {
visited[s] = true;
cout<<s;
for(int i = 0;i < rev_adj[s].size();++i){
if(visited[rev_adj[s][i]] == false)
rev_dfs(rev_adj[s][i]);
}
}
void dfs_util(){
for(int i = 0;i < nodes;++i) {
if(visited[i] == false) {
dfs(i);
}
}
}
void initialize() {
for(int i = 0;i <nodes;++i)
visited[i] = false;
}
void SCC(){
dfs_util();
initialize();
cout<<"Strongly Connected Components: "<<endl;
while(!st.empty()){
int h=st.top();
st.pop();
if(visited[h] == false){
cout<<l++<<". ";
rev_dfs(h);
cout<<endl;
}
}
}
int main() {
cin >> nodes;
cin >> edges;
for(int i = 0;i < edges;++i){
cin >> x >> y;
adj[x].push_back(y);
rev_adj[y].push_back(x);
}
initialize();
SCC();
}