跳到正文

算法·理论

无向图 Tarjan

kevin1616约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年2月2日
篇幅
约 7 分钟
获赞
1

版本

  1. v1当前

定义

  1. 割点(割顶):在无向图中,若删除点 xx 及其连边,使得连通块变多,那么 xx 为割点(割顶)。
  2. 割边(桥):在无向图中,若删除一条边 ee 后,使得连通块变多,那么 xx 为割边(桥)。
  3. 点双连通:若无向图点对 x,yx,y,删除任意非 xx 和非 yy 节点后,xx 和 yy 仍然联通,则称 x,yx,y 点双连通。
  4. 边双连通:若无向图点对 x,yx,y,删除任意一条边后,xx 和 yy 仍然联通,则称 x,yx,y 边双连通。
  5. 点双连通子图:无向图中的一个子图 GG,GG 中任意两点都是点双连通的,那么称 GG 为点双连通子图。
  6. 边双连通子图:无向图中的一个子图 GG,GG 中任意两点都是边双连通的,那么称 GG 为边双连通子图。
  7. 点双连通分量(V-DCC):无向图的极大点双连通子图成为点双连通分量。
  8. 边双连通分量(E-DCC):无向图的极大边双连通子图成为边双连通分量。

性质

  1. 无向图至少有 33 个点,才有可能有割点。
  2. 点双连通不具有传递性,边双连通具有传递性。
  3. dfs 搜索树中的根结点有 22 个及以上的子结点时,才可能是割点。
  4. 一个割点可能出现在多个点连通分量。
  5. 割边一定不在环上,环上的边必然不是割边。

判定

割点

  1. 维护 dfnxdfn_x 表示在一次 dfs 中点 xx 被遍历到的次序,称为时间戳。
  2. 维护 lowxlow_x 表示点 xx 在一次 dfs 中能够到达的最小时间戳。
  3. 当 dfs 中从 xx 走向 yy,且 lowy≥dfnxlow_y\ge dfn_x 时,xx 是割点。
  4. 此时 xx 不应是 dfs 的根结点,如果是,xx 应只有 yy 这一个子结点。

割边

  1. 维护 dfnxdfn_x 表示在一次 dfs 中点 xx 被遍历到的次序,称为时间戳。
  2. 维护 lowxlow_x 表示点 xx 在一次 dfs 中能够到达的最小时间戳。
  3. 当 dfs 中从 xx 走向 yy,且 lowy>dfnxlow_y>dfn_x 时,x→yx\rightarrow y 是割边。
  4. 递归时,不可以走成对的反向边。

代码

查找所有割点

C++
#include<bits/stdc++.h>
using namespace std;
int n,m;
vector <int> ve[20005];
int dfn[20005],tot;
int low[20005];
int cut[20005];
int cnt,root;
void Tarjan(int x){
	dfn[x] = low[x] = ++tot;
	int flag = 0;
	for(int it : ve[x]){
		if(!dfn[it]){
			Tarjan(it);
			low[x] = min(low[x],low[it]);
			if(low[it] >= dfn[x]){
				flag++;
				if(x != root || flag >= 2) cut[x] = 1;
			}
		}
		else low[x] = min(low[x],dfn[it]);
	}
	return;
}
int main(){
	cin >> n >> m;
	for(int i = 1;i <= m;i++){
		int x,y;
		cin >> x >> y;
		if(x == y) continue;
		ve[x].push_back(y);
		ve[y].push_back(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]) cnt++;
	}
	cout << cnt << "\n";
	for(int i = 1;i <= n;i++){
		if(cut[i]) cout << i << " ";
	}
	return 0;
}

查找所有点双连通分量

C++
#include<bits/stdc++.h>
using namespace std;
const int NR = 5e5 + 5;
const int MR = 2e6 + 5;
int head[MR],ver[MR * 2],Next[MR * 2];
int dfn[NR],low[NR];
stack <int> st;
vector <int> vdcc[NR];
int dcc;
int n,m;
int tot = 1,num;
int root;
bool cut[NR];
void add(int x,int y){
	ver[++tot] = y;
	Next[tot] = head[x];
	head[x] = tot;
}
void Tarjan(int x){
	dfn[x] = low[x] = ++num;
	st.push(x);
	if(x == root && head[x] == 0){
		vdcc[++dcc].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) cut[x] = 1;
				dcc++;
				int tmp = 0;
				while(tmp != y){
					tmp = st.top();
					st.pop();
					vdcc[dcc].push_back(tmp);
				}
				vdcc[dcc].push_back(x);
			}
		}
		else low[x] = min(low[x],dfn[y]);
	}
}
int main(){
	cin >> n >> m;
	for(int i = 1;i <= m;i++){
		int x,y;
		cin >> 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);
		}
	}
	cout << dcc << "\n";
	for(int i = 1;i <= dcc;i++){
		cout << vdcc[i].size() << " ";
		for(int it : vdcc[i]) cout << it << " ";
		cout << "\n";
	}
	return 0;
}

总结

Tarjan 是一个功能强大的算法,在后续的算法也有许多应用。


练习

P8435 【模板】点双连通分量

P3388 【模板】割点(割顶)

P1656 炸铁路

原文