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 发布
本文深入讲解了Tarjan算法,一种用于图论中寻找强连通分量的经典算法。通过使用深度优先搜索和栈数据结构,Tarjan算法能够高效地解决有向图的强连通分量分解问题。

1446

被折叠的 条评论
为什么被折叠?



