Tarjan 求强连通分量 / 割点

本文深入讲解了Tarjan算法在求解强连通分量和割点问题中的应用,提供了详细的C++实现代码,帮助读者理解算法原理及其实现细节。

视频讲解戳我

 

tarjan 求强连通分量 基础模板:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int maxn=1e5+5;
vector<int> mp[maxn];
int dfn[maxn],low[maxn];
bool ins[maxn];
int tim,cnt;
stack<int> s;
int color[maxn],colornum[maxn];
int n,m;
void tarjan(int u){
	++tim;
	dfn[u]=low[u]=tim;
	s.push(u);
	ins[u]=true;
	for(int tp:mp[u]){
		if(dfn[tp]==0){
			tarjan(tp);
			low[u]=min(low[u],low[tp]);
		}
		else if(ins[tp]==true){
			low[u]=min(low[u],dfn[tp]);
		}
	}
	if(low[u]==dfn[u]){
		cnt++;
		while(s.top()!=u){
			int tp=s.top();
			s.pop();
			color[tp]=cnt;
			colornum[cnt]++;
			ins[tp]=false;
		}
		s.pop();
		color[u]=cnt;
		colornum[cnt]++;
		ins[u]=false;	
	}
}

int main(){
	std::ios::sync_with_stdio(0);
	cin>>n>>m;
	int x,y;
	for(int i=1;i<=m;++i){
		cin>>x>>y;
		mp[x].push_back(y);
	}
	for(int i=1;i<=n;++i){
		if(dfn[i]==0){
			tarjan(i);
		}
	}
	
	return 0;
}

tarjan 求割点 基础模板:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int maxn=2e4+5;
vector<int> mp[maxn];
int n,m;
int low[maxn];
int dfn[maxn];
int tim;
set<int> ans;
void tarjan(int u,int fa){
	++tim;
	low[u]=dfn[u]=tim;
	int child=0;
	for(int v:mp[u]){
		if(dfn[v]==0){
			child++;
			tarjan(v,fa);
			low[u]=min(low[u],low[v]);
			if(u!=fa && low[v]>=dfn[u]){
				ans.insert(u);
			}
		}
		else{
			low[u]=min(low[u],dfn[v]);
		}
	} 
	if(child>=2 && u==fa)	ans.insert(fa);
}
int main(){
	std::ios::sync_with_stdio(0);
	cin>>n>>m;
	int x,y;
	for(int i=1;i<=m;++i){
		cin>>x>>y;
		mp[x].push_back(y);
		mp[y].push_back(x);
	}
	for(int i=1;i<=n;++i){
		if(dfn[i]==0)	tarjan(i,i);
	}
	cout<<ans.size()<<endl;
	for(int i:ans){
		cout<<i<<' ';
	}
	return 0;
}

 

### Tarjan算法 Tarjan算法通过深度优先搜索(DFS)遍历图,利用DFS序和回溯边来判断。具体来说,如果一个节u存在至少一个子节v,使得v无法通过其他路径到达u的祖先节(即low[v] >= dfn[u]),则u为。对于根节,需要特殊处理,当它有两个或以上子节时,它才是。 ### Tarjan算法边的判断标准是,对于边(u, v),若low[v] > dfn[u],则(u, v)为边。这是因为v无法通过其他路径到达u及其祖先,所以删除边(u, v)会使得图不连通。 ### Tarjan算法解双连通分量 边双连通分量的解可以通过Tarjan算法找到所有的边,然后将这些边从图中移除,剩下的每个连通分量即为一个边双连通分量。双连通分量的解较为复杂,需要在DFS过程中维护一个栈,每当发现一个时,就将栈中的节弹出,直到当前节v被弹出为止,这些节构成了一个双连通分量。 ### 实现代码示例 以下是一个使用Tarjan算法边以及边双连通分量的Python代码示例: ```python def tarjan(u, parent): global index_counter dfn[u] = low[u] = index_counter index_counter += 1 child_count = 0 for v in adj[u]: if v == parent: continue if dfn[v] == -1: stack.append((u, v)) child_count += 1 tarjan(v, u) low[u] = min(low[u], low[v]) if low[v] > dfn[u]: # Edge (u, v) is a bridge bridges.append((u, v)) if low[v] >= dfn[u] and parent != -1: # Node u is an articulation point articulation_points.add(u) elif dfn[v] < dfn[u]: stack.append((u, v)) low[u] = min(low[u], dfn[v]) if parent == -1 and child_count > 1: # Node u is an articulation point (root case) articulation_points.add(u) # Initialization n = 5 # Number of nodes in the graph adj = [[] for _ in range(n)] # Adjacency list representation of the graph dfn = [-1] * n # Discovery time of nodes low = [-1] * n # Lowest discovery time reachable index_counter = 0 bridges = [] # List to store bridges articulation_points = set() # Set to store articulation points stack = [] # Stack to keep track of edges for biconnected components # Example graph construction adj[0] = [1, 2] adj[1] = [0, 2] adj[2] = [0, 1, 3, 4] adj[3] = [2, 4] adj[4] = [2, 3] for i in range(n): if dfn[i] == -1: tarjan(i, -1) print("Articulation Points:", articulation_points) print("Bridges:", bridges) ``` ### 边双连通分量的实现 为了找到边双连通分量,可以在找到所有边后,将这些边从图中移除,然后对剩余的边进行连通分量的查找。每个连通分量即为一个边双连通分量。 ### 双连通分量的实现 双连通分量的实现需要在Tarjan算法中使用栈来跟踪边,每当发现一个时,就将栈中的边弹出,直到当前边(u, v)被弹出为止,这些边构成了一个双连通分量。 ### 总结 通过上述方法,可以有效地使用Tarjan算法来解图中的边以及双连通分量问题。这种方法在处理大规模图时非常高效,因为它的时间复杂度是线性的,即O(V + E),其中V是顶数,E是边数[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值