forked from ccgcv/Cplus-plus-for-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKosaRaju's_Algorithm.cpp
101 lines (78 loc) · 1.89 KB
/
KosaRaju's_Algorithm.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<bits/stdc++.h>
using namespace std;
void base(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
vector<vector<int>> ans;
void dfs(int vertex, vector<vector<int>>& graph, vector<bool>& vis, stack<int>& st){
vis[vertex] = true;
for(auto x : graph[vertex]){
if(!vis[x]){
dfs(x, graph, vis, st);
}
}
st.push(vertex);
}
void new_dfs(int vertex, vector<bool>& vis, vector<vector<int>>& trans, vector<int>& v){
v.push_back(vertex);
vis[vertex] = 1;
for(auto x : trans[vertex]){
if(!vis[x]){
new_dfs(x, vis, trans, v);
}
}
}
int main(){
base();
// n = number of total nodes(nodes start with 0 t0 n) and e = number of total edges
int n,e;
cin >> n >> e;
// Create adjanacy list
vector<vector<int>> graph(n);
// taking inputs
for(int i = 0; i < e; i++) {
int node1,node2;
cin >> node1 >> node2;
// IN kosaRaju's Algorithm, we find strongly connected components in directed Graph
graph[node1].push_back(node2);
}
// first, Sort all the nodes in order (topological sort)
stack<int> st;
vector<bool> vis(n,false);
for(int i=0;i<n;i++){
if(!vis[i]){
dfs(i, graph, vis, st);
}
}
// clear vis array
for(int i = 0; i < n; i++) vis[i] = false;
// Now, transpose the adjanacy list
vector<vector<int>> trans(n);
for(int i = 0; i < n; i++){
for(auto x : graph[i]){
trans[x].push_back(i);
}
}
// Now, iterating stack st
while(!st.empty()){
int vertex = st.top();
st.pop();
if(!vis[vertex]){
vector<int> v;
new_dfs(vertex, vis, trans, v);
ans.push_back(v);
}
}
// Answer
cout << "Total Strongly Connected Components =" << " " << ans.size() << endl;
for(int i=0;i<ans.size();i++){
cout << i+1 << ". ";
for(int j=0;j<ans[i].size();j++){
cout << ans[i][j] << " ";
}
cout << endl;
}
return 0;
}