forked from zimpha/algorithmic-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCC.cc
37 lines (36 loc) · 928 Bytes
/
SCC.cc
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
#include <cstring>
#include <vector>
struct Tarjan {// index from 0 to n - 1
static const int N = 100000 + 10;
std::vector<int> SCC[N];
int low[N], dfn[N], col[N];
int stk[N], top, scc_cnt, sz;
void dfs(int x, const std::vector<int> G[]) {
low[x] = dfn[x] = ++sz;
stk[++top] = x;
for (auto &y: G[x]) {
if (!dfn[y]) {
dfs(y, G);
low[x] = std::min(low[x], low[y]);
}
else if (col[y] == -1) {
low[x] = std::min(low[x], dfn[y]);
}
}
if (dfn[x] == low[x]) {
SCC[scc_cnt++].clear();
do {
SCC[scc_cnt - 1].push_back(stk[top]);
col[stk[top]] = scc_cnt - 1;
} while (stk[top--] != x);
}
}
void run(int n,const std::vector<int> G[]) {
sz = top = scc_cnt = 0;
memset(dfn, 0, sizeof(*dfn) * n);
memset(col, -1, sizeof(*col) * n);
for (int i = 0; i < n; ++i) {
if (!dfn[i]) dfs(i,G);
}
}
};