边双联通分量缩点

注意不要忘记原点变成缩点后的新点

模板

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
int cnt, bel[maxn]; // important!
namespace Tarjan {
int tot, dfn[maxn], low[maxn], st[maxn], top, vis[maxn];
void clear(int n) {
tot = top = cnt = 0;
for (int i = 1; i <= n; i++) {
edge[i].clear(); dfn[i] = vis[i] = bel[i] = 0;
}
}
void dfs(int u, int f) {
dfn[u] = low[u] = ++tot;
st[++top] = u; vis[u] = 1;
int k = 0;
for (int& v: edge[u]) {
if (v == f && !k) {
k++; continue;
}
if (!dfn[v]) {
dfs(v, u); low[u] = min(low[u], low[v]);
} else if (vis[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
cnt++; int t = 0;
do {
t = st[top--];
bel[t] = cnt;
vis[t] = 0;
} while (t != u);
}
}
void scc(int n, vector<int> * g) {
for (int i = 1; i <= n; i++) if (!dfn[i]) Tarjan::dfs(i, 0);
for (int i = 1; i <= n; i++) g[i].clear();
for (int i = 1; i <= n; i++) {
int u = bel[i];
for (int& x: edge[i]) {
int v = bel[x];
if (u != v) {
g[u].push_back(v);
}
}
}
}
}
  • 本文作者: XLor
  • 本文链接: https://xlor.cn/2019/4/scc/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!