-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy path27_kosaraju_algo_count_strongly_connected_components.cpp
149 lines (116 loc) · 2.92 KB
/
27_kosaraju_algo_count_strongly_connected_components.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
link: https://practice.geeksforgeeks.org/problems/strongly-connected-components-kosarajus-algo/1#
sol (just refer): https://www.geeksforgeeks.org/strongly-connected-components/
video: https://youtu.be/V8qIqJxCioo?list=PLgUwDviBIf0rGEWe64KWas0Nryn7SCRWw
*/
// ----------------------------------------------------------------------------------------------------------------------- //
/*
TC: O(V) + O(V + E) + O(V + E), 1st for toposort, 2nd for transpose, 3rd for DFS acc. to finishing line
SC: O(V + E) + O(V) + O(V), 1st for storing transpose, 2nd for vis, 3rd for stack
*/
#include <bits/stdc++.h>
using namespace std;
void dfs(int node, stack<int>& st, vector<int>& vis, vector<int> adj[]) {
vis[node] = 1;
for (auto it : adj[node]) {
if (!vis[it]) {
dfs(it, st, vis, adj);
}
}
st.push(node);
}
void revDfs(int node, vector<int>& vis, vector<int> transpose[]) {
cout << node << " ";
vis[node] = 1;
for (auto it : transpose[node]) {
if (!vis[it]) {
revDfs(it, vis, transpose);
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<int> adj[n];
for (int i = 0;i < m;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
stack<int> st;
vector<int> vis(n, 0);
for (int i = 0;i < n;i++) {
if (!vis[i]) {
dfs(i, st, vis, adj);
}
}
vector<int> transpose[n];
for (int i = 0;i < n;i++) {
vis[i] = 0;
for (auto it : adj[i]) {
transpose[it].push_back(i);
}
}
while (!st.empty()) {
int node = st.top();
st.pop();
if (!vis[node]) {
cout << "SCC: ";
revDfs(node, vis, transpose);
cout << endl;
}
}
return 0;
}
/*
5 5
0 1
1 2
2 0
1 3
3 4
*/
// ----------------------------------------------------------------------------------------------------------------------- //
/*
[accepted]
*/
void dfs(int curr, stack<int>& st, vector<int>& vis, vector<int> adj[]) {
vis[curr] = 1;
for (auto i : adj[curr]) {
if (!vis[i]) dfs(i, st, vis, adj);
}
st.push(curr);
}
void revDfs(int curr, vector<int>& vis, vector<int> transpose[]) {
vis[curr] = 1;
for (auto it : transpose[curr]) {
if (!vis[it]) revDfs(it, vis, transpose);
}
}
int kosaraju(int V, vector<int> adj[])
{
stack<int> st;
vector<int> vis(V, 0);
for (int i = 0; i < V; i++) {
if (!vis[i]) {
dfs(i, st, vis, adj);
}
}
vector<int> transpose[V];
for (int i = 0; i < V; i++) {
vis[i] = 0;
for (auto it : adj[i]) {
transpose[it].push_back(i);
}
}
int cnt = 0;
while (!st.empty()) {
int node = st.top();
st.pop();
if (!vis[node]) {
cnt++;
revDfs(node, vis, transpose);
}
}
return cnt;
}