POJ 3713 Transferring Sylla(强联通分量割点)

Transferring Sylla
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3203 Accepted: 886

Description

After recapturing Sylla, the Company plans to establish a new secure system, a transferring net! The new system is designed as follows:

The Company staff choose N cities around the nation which are connected by "security tunnels" directly or indirectly. Once a week, Sylla is to be transferred to another city through the tunnels. As General ordered, the transferring net must reach a certain security level that there are at least 3 independent paths between any pair of cities ab. When General says the paths are independent, he means that the paths share only a and b in common.

Given a design of a transferring net, your work is to inspect whether it reaches such security level.

Input

The input consists of several test cases.
For each test case, the first line contains two integers, N ≤ 500 and M ≤ 20000. indicating the number of cities and tunnels.
The following M lines each contains two integers a and b (0 ≤ a, b < N), indicating the city a and city b are connected directly by a tunnel.

The input ends by two zeroes.

Output

For each test case output "YES" if it reaches such security level, "NO" otherwise.

Sample Input

4 6
0 1
0 2
0 3
1 2
1 3
2 3

4 5
0 1
0 2
0 3
1 2
1 3

7 6
0 1
0 2
0 3
1 2
1 3
2 3

0 0

Sample Output

YES
NO

NO

题解:
强联通分量割点,枚举删去每一个点,判断是否有割点。若有割点则一定不是三联通。

代码:

#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=505;
int n,m,head[maxn],tol;
int dfn[maxn],low[maxn],par[maxn];
bool bb;
struct node
{
	int to,next;
}rode[40002];
void add(int a,int b)
{
	rode[tol].to=b;
	rode[tol].next=head[a];
	head[a]=tol++;
}
int find(int x)
{
	if(par[x]==x)return x;
	return par[x]=find(par[x]);
}
void unite(int x,int y)
{
	x=find(x),y=find(y);
	if(x!=y)par[x]=y;
}
void tarjan(int x,int pre,int id)
{
	if(!bb)return;
	dfn[x]=low[x]=tol++;
	int child=0;
	for(int i=head[x];i!=-1;i=rode[i].next)
	{
		node e=rode[i];
		if(e.to==pre||e.to==id)continue;
		if(dfn[e.to]==0)
		{
			child++;
			tarjan(e.to,x,id);
			low[x]=min(low[x],low[e.to]);
			if(x!=pre&&low[e.to]>=dfn[x])bb=0;
		}
		else low[x]=min(low[x],dfn[e.to]);
	}
	if(pre==x&&child>1)bb=0;
}
int main()
{
	while(~scanf("%d%d",&n,&m)&&(n+m))
	{
		memset(head,-1,sizeof(head));
		for(int i=0;i<maxn;i++)par[i]=i;
		tol=1;
		for(int i=0;i<m;i++)
		{
			int x,y;scanf("%d%d",&x,&y);
			add(x,y),add(y,x),unite(x,y);
		}
		bb=1;
		for(int i=1;i<n;i++)
			if(find(0)!=find(i))bb=0;
		for(int i=0;i<n;i++)
		{
			if(!bb)break;
			memset(dfn,0,sizeof(dfn));tol=1;
			if(i==0)tarjan(1,1,i);
			else tarjan(0,0,i);
		}
		if(bb)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}
	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值