图论--SCC强连通缩点--Tarjan
生活随笔
收集整理的這篇文章主要介紹了
图论--SCC强连通缩点--Tarjan
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
強連通縮點與雙連通縮點大同小異,也就是說將強連通分支縮成一個點之后,沒有強連通,成為有向無環圖,在對圖進行題目的操作。
// Tarjan算法求有向圖強連通分量并縮點 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<queue> using namespace std; const int N = 100010, M = 1000010; int ver[M], Next[M], head[N], dfn[N], low[N]; int stack[N], ins[N], c[N]; int vc[M], nc[M], hc[N], tc; vector<int> scc[N]; int n, m, tot, num, top, cnt;void add(int x, int y) {ver[++tot] = y, Next[tot] = head[x], head[x] = tot; }void add_c(int x, int y) {vc[++tc] = y, nc[tc] = hc[x], hc[x] = tc; }void tarjan(int x) {dfn[x] = low[x] = ++num;stack[++top] = x, ins[x] = 1;for (int i = head[x]; i; i = Next[i])if (!dfn[ver[i]]) {tarjan(ver[i]);low[x] = min(low[x], low[ver[i]]);}else if (ins[ver[i]])low[x] = min(low[x], dfn[ver[i]]);if (dfn[x] == low[x]) {cnt++; int y;do {y = stack[top--], ins[y] = 0;c[y] = cnt, scc[cnt].push_back(y);} while (x != y);} }int main() {cin >> n >> m;for (int i = 1; i <= m; i++) {int x, y;scanf("%d%d", &x, &y);add(x, y);}for (int i = 1; i <= n; i++)if (!dfn[i]) tarjan(i);for (int x = 1; x <= n; x++)for (int i = head[x]; i; i = Next[i]) {int y = ver[i];if (c[x] == c[y]) continue;add_c(c[x], c[y]);} }?
總結
以上是生活随笔為你收集整理的图论--SCC强连通缩点--Tarjan的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 铠侠 USB 闪存盘结束对于 macOS
- 下一篇: 英国考虑 2030 年前推出数字英镑