跳到正文

个人记录

tarjan求联通分量整理

zzr8178541919约 13 分钟
原文
文章信息与版本

文章信息

发布
2021年1月8日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 13 分钟

版本

  1. v1当前

Tarjan是一个”神奇”的人物,它创造出来了大量的NOIP经典考点。


之前只学习了一点有向图的tarjantarjan算法以及有向图中的缩点,但对整个算法的具体流程以及原因并不是很清楚,无向图的tarjantarjan更是知之甚少。

因此我今天来重新学习。

下面我将对tarjantarjan做一个小小的总结:


割边:

割边指的就是对于一个连通的无向图,割掉某一条边后,能使图不再连通,那这条边就叫作割边。

模板codecode:

C++
#include<bits/stdc++.h>
#define int long long
#define reg register
using namespace std;
const int maxn=2e5+5;
const int mod=1e9+7;
const int INF=2e16;
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')
        f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//vector<int> v[maxn];
int n,m;
struct node
{
	int to;
	int nxt;
}edge[maxn];
int wugui=1;
int head1[maxn];
void add(int x,int y)
{
	wugui++;
	edge[wugui].to=y;
	edge[wugui].nxt=head1[x];
	head1[x]=wugui;
}
int dfn[maxn];
int low[maxn];
int cnt=0;
int bridge[maxn];
void tarjan(int x,int root)
{
	dfn[x]=low[x]=++cnt;
	for(int i=head1[x];i;i=edge[i].nxt)
	{
		int t=edge[i].to;
		if(dfn[t]==0)
		{
			tarjan(t,i);
			low[x]=min(low[x],low[t]);
			if(low[t]>dfn[x])
			{
				bridge[i]=bridge[i ^ 1]=1;
			}
		}
		else
		if(i!=(root ^ 1))
		{
			low[x]=min(low[x],dfn[t]);
		}
	}
}
signed main()
{ 
	n=read(),m=read();
	for(int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		add(x,y);
		add(y,x);
	}
	for(int i=1;i<=n;i++)
	{
		if(dfn[i]==0)
		tarjan(i,0);
	}
	for(int i=2;i<=wugui;i+=2)
	{
		if(bridge[i])
		{
			printf("%lld %lld\n",edge[i ^ 1].to,edge[i].to);
		}
	}
	return 0;
}
/*
5 7 1
1 2 4
1 4 4
2 5 1
2 3 2
3 4 2  
4 2 2
5 3 3
*/

割点

割点指的就是对于一个连通的无向图,割掉某一个点以及与这个点相连的所有的边后,能使图不再连通,那这个点就叫作割点。

模板codecode:

C++
#include<bits/stdc++.h>
#define int long long
#define reg register
using namespace std;
const int maxn=2e5+5;
const int mod=1e9+7;
const int INF=2e16;
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')
        f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
int n,m;
struct node
{
	int to;
	int nxt;
}edge[maxn];
int wugui=1;
int head1[maxn];
void add(int x,int y)
{
	wugui++;
	edge[wugui].to=y;
	edge[wugui].nxt=head1[x];
	head1[x]=wugui;
}
int dfn[maxn];
int low[maxn];
int cnt=0;
int root=0;
int cut[maxn];
void tarjan(int x)
{
	dfn[x]=low[x]=++cnt;
	int wns=0;
	for(int i=head1[x];i;i=edge[i].nxt)
	{
		int t=edge[i].to;
		if(dfn[t]==0)
		{
			tarjan(t);
			low[x]=min(low[x],low[t]);
			if(low[t]>=dfn[x])
			{	
				wns++;
				if(x!=root || wns>1)
				cut[x]=1;
			}
		}
		else
		low[x]=min(low[x],dfn[t]);
	}
}
signed main()
{ 
	n=read(),m=read();
	for(reg int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		add(x,y);
		add(y,x);
	}
	for(reg int i=1;i<=n;i++)
	{
		if(dfn[i]==0)
		{
			root=i;
			tarjan(i);
		}
	}
	for(reg int i=1;i<=n;i++)
	{
		if(cut[i])
		{
			printf("%lld ",i);
		}
	}
	printf("\n");
	return 0;
}
/*
5 7 1
1 2 4
1 4 4
2 5 1
2 3 2
3 4 2  
4 2 2
5 3 3
*/

边双连通分量

定义:很简单,就是不存在割边的分量。

那它有一个很重要的性质:任意一条边都属于至少一个简单环中。

模板code:code:

C++
#include<bits/stdc++.h>
#define int long long
#define reg register
using namespace std;
const int maxn=2e5+5;
const int mod=1e9+7;
const int INF=2e16;
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')
        f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//vector<int> v[maxn];
int n,m;
struct node
{
	int to;
	int nxt;
}edge[maxn];
int wugui=1;
int head1[maxn];
void add(int x,int y)
{
	wugui++;
	edge[wugui].to=y;
	edge[wugui].nxt=head1[x];
	head1[x]=wugui;
}
int dfn[maxn];
int low[maxn];
int cnt=0;
int bridge[maxn];
void tarjan(int x,int root)
{
	dfn[x]=low[x]=++cnt;
	for(int i=head1[x];i;i=edge[i].nxt)
	{
		int t=edge[i].to;
		if(dfn[t]==0)
		{
			tarjan(t,i);
			low[x]=min(low[x],low[t]);
			if(low[t]>dfn[x])
			{
				bridge[i]=bridge[i ^ 1]=1;
			}
		}
		else
		if(i!=(root ^ 1))
		{
			low[x]=min(low[x],dfn[t]);
		}
	}
}
int col[maxn];
int color=0;
void dfs(int x)
{
	col[x]=color;
	for(int i=head1[x];i;i=edge[i].nxt)
	{
		int t=edge[i].to;
		if(bridge[i] || col[t])
		continue;
		dfs(t);
	}
}
signed main()
{ 
	n=read(),m=read();
	for(int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		add(x,y);
		add(y,x);
	}
	for(int i=1;i<=n;i++)
	{
		if(dfn[i]==0)
		tarjan(i,0);
	}
	for(int i=2;i<=wugui;i+=2)
	{
		if(bridge[i])
		{
			printf("%lld %lld\n",edge[i ^ 1].to,edge[i].to);
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(col[i]==0)
		{	
			color++;
			dfs(i);
		}		
	}
	for(int i=1;i<=n;i++)
	{
		printf("%lld ",col[i]);
	}
	printf("\n");
	return 0;
}

点双连通分量:

点双连通分量稍微复杂一些。

定义:就是不存在割点的分量。

它也有一个很重要的性质:任意两个点都同时包含在至少一个简单环中。

对于点双连通分量的缩点,这里要强调一下:在构建的时候它新加了k个节点,k为图中割点的个数。

为什么要新建点呢?

因为同一个割点可能同时属于多个点双连通分量。

用新建的节点和它的所有的点双连通分量的子树相连,就能得到一棵新树。

模板code:code:

C++
#include<bits/stdc++.h>
#define int long long
#define reg register
using namespace std;
const int maxn=2e5+5;
const int mod=1e9+7;
const int INF=2e16;
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')
        f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
int n,m;
struct node
{
	int to;
	int nxt;
}edge[maxn];
int wugui=1;
int head1[maxn];
void add(int x,int y)
{
	wugui++;
	edge[wugui].to=y;
	edge[wugui].nxt=head1[x];
	head1[x]=wugui;
}
int dfn[maxn];
int low[maxn];
int cnt=0;
int root=0;
int cut[maxn];
vector<int> v[maxn];
int color=0;
int s[maxn];
int tot=0;
int col[maxn];
void tarjan(int x)
{
	dfn[x]=low[x]=++cnt;	
	if(x==root && head1[x]==0)
	{
		color++;
		v[color].push_back(x);
		return; 
	}
	s[++tot]=x;
	int wns=0;
	for(reg int i=head1[x];i;i=edge[i].nxt)
	{
		int t=edge[i].to;
		if(dfn[t]==0)
		{
			tarjan(t);
			low[x]=min(low[x],low[t]);
			if(low[t]>=dfn[x])
			{	
				wns++;
				if(x!=root || wns>1)
					cut[x]=1;
				color++;
				int y;
				do
				{
					y=s[tot];
					v[color].push_back(y);
					tot--;
				}
				while(y!=t);
				v[color].push_back(x);
			}
		}
		else
		low[x]=min(low[x],dfn[t]);
	}
}
signed main()
{ 
	n=read(),m=read();
	for(reg int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		add(x,y);
		add(y,x);
	}
	for(reg int i=1;i<=n;i++)
	{
		if(dfn[i]==0)
		{
			root=i;
			tarjan(i);
		}
	}
	for(reg int i=1;i<=color;i++)
	{
		printf("%lld\n",i);
		for(reg int j=0;j<(int)v[i].size();j++)
		{
			printf("%lld ",v[i][j]);
		}
		printf("\n");
	}
	return 0;
}
/*
5 7 1
1 2 4
1 4 4
2 5 1
2 3 2
3 4 2  
4 2 2
5 3 3
*/
原文