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 a, b. 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;
}