【模板】Tarjan强联通分量
C++版本:
Tarjan强联通分量
void tarjan(int u) {
int v;
dfn[u] = low[u] = ++in;
instack[u] = true;
Stap[++stop] = u;
for (int i = head[u]; i; i = eg[i].nex) {
v = eg[i].to;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[v], low[u]);
}
else if (instack[v])
low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
++Bcnt;
while (true) {
v = Stap[stop--];
instack[v] = false;
belong[v] = Bcnt;
all[Bcnt]++;
if (v == u)break;
}
}
}