-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCC.cpp
More file actions
83 lines (67 loc) · 1.46 KB
/
SCC.cpp
File metadata and controls
83 lines (67 loc) · 1.46 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector<vector<int>> adj(N), adj_rev(N);
bool vis[N];
vector<int> order, component, root_nodes, roots(N), adj_scc[N];
void dfs1(int u) {
vis[u] = true;
for (auto &v : adj[u]) {
if (!vis[v]) {
dfs1(v);
}
}
order.push_back(u);
}
void dfs2(int u) {
vis[u] = true;
component.push_back(u);
for (auto &v : adj_rev[u]) {
if (!vis[v]) {
dfs2(v);
}
}
}
void scc(int n) {
order.clear();
root_nodes.clear();
for (int u = 0; u < n; u++) {
if (!vis[u]) {
dfs1(u);
}
}
reverse(order.begin(), order.end());
fill(vis, vis + n, false);
for (auto &u : order) {
if (!vis[u]) {
dfs2(u);
int root = component.front();
for (auto &v : component) {
roots[v] = root;
}
root_nodes.push_back(root);
adj_scc[root].clear();
component.clear();
}
}
for (int u = 0; u < n; u++) {
for (auto &v : adj[u]) {
int root_u = roots[u], root_v = roots[v];
if (root_u != root_v) {
adj_scc[root_u].push_back(root_v);
}
}
}
}
void solve() {
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}