跳到正文

算法·理论

塔杨模板

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

文章信息

收录
2026年7月21日
更新
2026年7月27日
篇幅
约 7 分钟
获赞
1

版本

  1. v1当前

塔杨缩环

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
vector<int> e[10009];
int dfn[10009],low[10009],tot,stk[10009],instk[10009],top;
int scc[10009],cnt;
void tarjan(int x){
	dfn[x]=low[x]=++tot;
	stk[++top]=x;
	instk[x]=1;
	for(int y : e[x]){
		if(!dfn[y]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if(instk[y]) low[x]=min(low[x],dfn[y]);
	}
	if(dfn[x]==low[x]){
		int y;++cnt;
		do{
			y=stk[top--];
			instk[y]=0;
			scc[y]=cnt;
		}while(y!=x);
	}
}
signed main(){
	//
	return 0;
}

塔杨割边

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
struct node{
	int u,v;
}bri[2009];
vector<node> e;
vector<int> h[1009];
int dfn[1009],low[1009],tot,cnt;
void add(int a,int b){
	e.push_back({a,b});
	h[a].push_back(e.size()-1);
}
void tarjan(int x,int iedg){
	dfn[x]=low[x]=++tot;
	for(int i=0;i<h[x].size();i++){
		int j=h[x][i];
		int y=e[j].v;
		if(!dfn[y]){
			tarjan(y,j);
			low[x]=min(low[x],low[y]);
			if(low[y]>dfn[x]){
				bri[++cnt]={x,y};
			}
		}
		else if(j!=(iedg^1)) low[x]=min(low[x],dfn[y]);
	}
}
signed main(){
	//
	return 0;
}

塔杨割点

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
vector<int> e[1009];
int dfn[1009],low[1009],tot,cut[1009],root;
void tarjan(int x){
	dfn[x]=low[x]=++tot;
	int child=0;
	for(int y : e[x]){
		if(!dfn[y]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
			if(low[y]>=dfn[x]){
				child++;
				if(x!=root||child>1) cut[x]=1;
			}
		}
		else low[x]=min(low[x],dfn[y]);
	}
}
signed main(){
	//
	return 0;
}

塔杨边双连通

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
struct node{
	int v,ne;
}e[2009];
int h[1009],idx=1;
int dfn[1009],low[1009],tot;
stack<int> stk;
int dcc[1009],cnt,bri[2009],d[1009];
void add(int a,int b){
	e[++idx].v=b;e[idx].ne=h[a];
	h[a]=idx;
}
void tarjan(int x,int iedg){
	dfn[x]=low[x]=++tot;
	stk.push(x);
	for(int i=h[x];i;i=e[i].ne){
		int y=e[i].v;
		if(!dfn[y]){
			tarjan(y,i);
			low[x]=min(low[x],low[y]);
			if(low[y]>dfn[x]){
				bri[i]=bri[i^1]=1;
			}
		}
		else if(i!=(iedg^1)) low[x]=min(low[x],dfn[y]);
	}
	if(dfn[x]==low[x]){
		++cnt;
		while(1){
			int y=stk.top();stk.pop();
			dcc[y]=cnt;
			if(y==x) break;
		}
	}
}
signed main(){
	//
	return 0;
}

塔杨点双连通

C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
vector<int> e[500009],ne[500009],dcc[500009];
int dfn[500009],low[500009],tot;
stack<int> stk;
int cut[500009],root,cnt,num,id[500009];
int n,m;
void tarjan(int x){
	dfn[x]=low[x]=++tot;
	stk.push(x);
	if(!e[x].size()){
		dcc[++cnt].push_back(x);
		return;
	}
	int child=0;
	for(int y : e[x]){
		if(!dfn[y]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
			if(low[y]>=dfn[x]){
				child++;
				if(x!=root||child>1) cut[x]=1;
				cnt++;
				while(1){
					int z=stk.top();stk.pop();
					dcc[cnt].push_back(z);
					if(z==y)break;
				}
				dcc[cnt].push_back(x);
			}
		}
		else low[x]=min(low[x],dfn[y]);
	}
}
signed main(){
	cin>>n>>m;
	while(m--){
		int a,b;
		cin>>a>>b;
		if(a==b) continue;
		e[a].push_back(b);
		e[b].push_back(a);
	}
	for(root=1;root<=n;root++){
		if(!dfn[root]) tarjan(root);
	}
	num=cnt;
	for(int i=1;i<=n;i++){
		if(cut[i]){
			id[i]=++num;
		}
	}
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;i++){
		cout<<dcc[i].size()<<' ';
		for(int j=0;j<dcc[i].size();j++){
			int x=dcc[i][j];
			cout<<x<<' ';
		}
		cout<<endl;
	}
	return 0;
}
原文