-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSCC.cpp
More file actions
56 lines (55 loc) · 1.14 KB
/
SCC.cpp
File metadata and controls
56 lines (55 loc) · 1.14 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
// Strongly Connected Components (Tarjan's SCC)
#include<bits/stdc++.h>
using namespace std;
int dfn[200005];
int low[200005];
int stk[200005];
int pa[200005];
int scc;
int idx;
stack<int>st;
vector<int>g[200005];
void tarjan(int now) {
dfn[now] = low[now] = ++idx;
stk[now] = 1;
st.push(now);
for (auto &i : g[now]) {
if (!dfn[i]) {
tarjan(i);
low[now] = min(low[now], low[i]);
}
else if (stk[i]) {
low[now] = min(low[now], dfn[i]);
}
}
if (dfn[now] == low[now]) {
scc++;
int nxt =-1;
while (nxt != now) {
nxt = st.top();
st.pop();
stk[nxt] = 0;
pa[nxt] = scc;
}
}
}
void solve() {
// tarjan scc
int n, m;
cin >> n >> m;
for (int i = 0 ; i < m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
}
for (int i = 1 ; i <= n ; i++) {
if (!dfn[i]) {
tarjan(i);
}
}
cout << scc << '\n'; // scc count
for (int i = 1 ; i <= n ; i++) {
cout << pa[i] << ' ';// scc number
}
cout << '\n';
}