-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscc_tarjan.cpp
50 lines (50 loc) · 1.35 KB
/
scc_tarjan.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
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 150010;
const int M = 300010;
struct Edge
{
int head[N], nxt[M], pnt[M], E;
void init() {
E = 0;
memset(head, -1, sizeof(head));
}
void add_edge(int a, int b){
pnt[E] = b;
nxt[E] = head[a];
head[a] = E++;
}
}ori, ne, rne;
int in[N], bel[N], dfn[N], low[N], stk[N], n, m, Time, top, btype;
std::vector<int> con[N];
void dfs(int u)
{
low[u] = dfn[u] = ++Time; in[u] = 1; stk[++top] = u;
for(int i = ori.head[u]; ~i; i = ori.nxt[i]) {
int v = ori.pnt[i];
if(!dfn[v]) {
dfs(v);
low[u]=std::min(low[u], low[v]);
} else if(in[v]) {
low[u]=std::min(low[u], low[v]);
}
}
if(low[u] == dfn[u]) {
btype++; int v;
do {
v = stk[top--], in[v] = 0, bel[v] = btype;
con[btype].push_back(v);
}while(v != u);
}
}
void scc()
{
std::fill(in, in + n + 1, 0);
std::fill(dfn, dfn + n + 1, 0);
btype = Time = top = 0;
for(int i = 1; i <= n; i++) if(!dfn[i]) {
dfs(i);
}
}