int dfn[maxn], low[maxn], scc[maxn], tot, scc_cnt;
stack<int> st;
void tarjan(int x) {
dfn[x] = low[x] = ++tot;
st.push(x);
for (auto v:G[x]) {
if (!dfn[v]) {
tarjan(v);
low[x] = min(low[x], low[v]);
} else if (!scc[v]) {
low[x] = min(low[x], dfn[v]);
}
}
if (dfn[x] == low[x]) {
++scc_cnt;
int v;
while (v != x) {
v = st.top();
st.pop();
scc[v] = scc_cnt;
}
}
}
void findScc() {
tot = scc_cnt = 0;
memset(dfn, 0, sizeof(dfn));
memset(scc, 0, sizeof(scc));
for (int i = 1; i <= n; ++i) {
if (!dfn[i]) {
tarjan(i);
}
}
}
Tarjan求强连通分量模板
最新推荐文章于 2025-03-22 20:45:19 发布