当前位置: 代码迷 >> 综合 >> 无向图(可重边):割点 桥 边连通分量 点连通分量 缩点 tarjan算法模板
  详细解决方案

无向图(可重边):割点 桥 边连通分量 点连通分量 缩点 tarjan算法模板

热度:79   发布时间:2024-01-15 07:12:44.0

代码模板来自《算法竞赛进阶指南》强烈推荐,原理讲的很好

tarjan算法求无向图(可有重边)的桥、边双连通分量并缩点

///tarjan算法求无向图(可有重边)的桥、边双连通分量并缩点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int SIZE = 100010;
int head[SIZE], ver[SIZE * 2], Next[SIZE * 2];
///head[i]=x表示以i起点的数组下标,ver[x]表示以i起点的终点编号
///next[x]=y下一个表示以i起点的数组下标 ,ver[y]表示另一个以i起点的终点编号
int dfn[SIZE], low[SIZE], c[SIZE];
///dfn表示时间戳
///low表示追溯值,c[x]表示结点x属于边连通分量的编号
int n, m, tot, num, dcc, tc;   
bool bridge[SIZE * 2]; ///是否是桥
int hc[SIZE], vc[SIZE * 2], nc[SIZE * 2];
///与上面未缩点表示含义一样,只不过把e——dcc看成一个结点
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, int in_edge) {dfn[x] = low[x] = ++num;for (int i = head[x]; i; i = Next[i]) {///遍历每一个结点int y = ver[i];if (!dfn[y]) {tarjan(y, i);low[x] = min(low[x], low[y]);if (low[y] > dfn[x]) ///找到桥bridge[i] = bridge[i ^ 1] = true;}else if (i != (in_edge ^ 1)) ///利用异或性质解决重边///防止x结点到父亲结点low[x] = min(low[x], dfn[y]);}
}void dfs(int x) {c[x] = dcc;for (int i = head[x]; i; i = Next[i]) {int y = ver[i];if (c[y] || bridge[i]) continue;dfs(y);}
}int main() {cin >> n >> m;tot = 1;for (int i = 1; i <= m; i++) {int x, y;scanf("%d%d", &x, &y);add(x, y), add(y, x);}for (int i = 1; i <= n; i++)if (!dfn[i])tarjan(i, 0);for (int i = 2; i < tot; i += 2)if (bridge[i])printf("%d %d\n", ver[i ^ 1], ver[i]);for (int i = 1; i <= n; i++)if (!c[i]) {++dcc;dfs(i);}printf("There are %d e-DCCs.\n", dcc);for (int i = 1; i <= n; i++)printf("%d belongs to DCC %d.\n", i, c[i]);///缩点tc = 1;for (int i = 2; i <= tot; i++) {int x = ver[i ^ 1], y = ver[i];if (c[x] == c[y]) continue;add_c(c[x], c[y]);}printf("缩点之后的森林,点数 %d,边数 %d(可能有重边)\n", dcc, tc / 2);for (int i = 2; i < tc; i += 2)printf("%d %d\n", vc[i ^ 1], vc[i]);
}

tarjan算法求无向图的割点、点双连通分量并缩点

// tarjan算法求无向图的割点、点双连通分量并缩点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int SIZE = 100010;
int head[SIZE], ver[SIZE * 2], Next[SIZE*2];
///head[i]=x表示以i起点的数组下标,ver[x]表示以i起点的终点编号
///next[x]=y下一个表示以i起点的数组下标 ,ver[y]表示另一个以i起点的终点编号
int dfn[SIZE], low[SIZE], stack[SIZE], new_id[SIZE], c[SIZE];
///dfn表示时间戳
///low表示追溯值,dcc[x]表示结点x属于点连通分量的编号
int n, m, tot, num, root, top, cnt, tc;
bool cut[SIZE];
vector<int> dcc[SIZE];
int hc[SIZE], vc[SIZE * 2], nc[SIZE * 2];
///与上面未缩点表示含义一样,只不过把v——dcc看成一个结点
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;if (x == root && head[x] == 0) { // 孤立点dcc[++cnt].push_back(x);return;}int flag = 0;for (int i = head[x]; i; i = Next[i]) {int y = ver[i];//子节点if (!dfn[y]) {tarjan(y);low[x] = min(low[x], low[y]);if (low[y] >= dfn[x]) {flag++;if (x != root || flag > 1) ///除了孤立结点,双连通分量至少为2cut[x] = true;        cnt++;int z;do {z = stack[top--];dcc[cnt].push_back(z);} while (z != y);dcc[cnt].push_back(x);}}else low[x] = min(low[x], dfn[y]);}
}int main() {cin >> n >> m;tot = 1;for (int i = 1; i <= m; i++) {int x, y;scanf("%d%d", &x, &y);if (x == y) continue;add(x, y), add(y, x);}for (int i = 1; i <= n; i++)if (!dfn[i]) root = i, tarjan(i);for (int i = 1; i <= n; i++)if (cut[i]) printf("%d ", i);puts("are cut-vertexes");for (int i = 1; i <= cnt; i++) {printf("v-DCC #%d:", i);for (int j = 0; j < dcc[i].size(); j++)printf(" %d", dcc[i][j]);puts("");}// 给每个割点一个新的编号(编号从cnt+1开始)num = cnt;for (int i = 1; i <= n; i++)if (cut[i]) new_id[i] = ++num;// 建新图,从每个v-DCC到它包含的所有割点连边tc = 1;for (int i = 1; i <= cnt; i++)for (int j = 0; j < dcc[i].size(); j++) {int x = dcc[i][j];if (cut[x]) {add_c(i, new_id[x]);add_c(new_id[x], i);}else c[x] = i; // 除割点外,其它点仅属于1个v-DCC}printf("缩点之后的森林,点数 %d,边数 %d\n", num, tc / 2);printf("编号 1~%d 的为原图的v-DCC,编号 >%d 的为原图割点\n", cnt, cnt);for (int i = 2; i < tc; i += 2)printf("%d %d\n", vc[i ^ 1], vc[i]);
}