poj 3713(判断无向图是否三连通)

Transferring Sylla
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1160 Accepted: 270

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

Source

分析:三连通没想法阿,看别人的题解都是枚举去掉一个点,然后判断是否存在割点或并且是否连通,如果存在割点或者不连通,就不是三连通。。。很奇怪阿,时间太恶心了。不知道那几个几百ms的怎么来的
代码:
#include<cstdio>
using namespace std;
const int mm=44444;
const int mn=555;
int t[mm],p[mm];
int h[mn],dfn[mn],low[mn],du[mn];
int i,j,k,n,m,idx;
bool dfs(int u,int fa)
{
    dfn[u]=low[u]=++idx;
    for(int i=h[u],v,son=0;i>=0;i=p[i])
        if(!dfn[v=t[i]])
        {
            ++son;
            if(dfs(v,u))return 1;
            if(fa==-1&&son>1||fa!=-1&&dfn[u]<=low[v])return 1;
            if(low[u]>low[v])low[u]=low[v];
        }
        else if(v!=fa&&low[u]>dfn[v])low[u]=dfn[v];
    return 0;
}
bool tarjan()
{
    int i,j,k;
    for(i=0;i<n;++i)
        if(du[i]<3)return 0;
    for(i=0;i<n;++i)
    {
        for(j=idx=0;j<n;++j)dfn[j]=0;
        dfn[i]=n+n;
        for(j=k=0;j<n;++j)
            if(!dfn[j])
            {
                if(++k>1)return 0;
                if(dfs(j,-1))return 0;
            }
    }
    return 1;
}
int main()
{
    while(scanf("%d%d",&n,&m),n+m)
    {
        for(i=k=0;i<n;++i)h[i]=-1,du[i]=0;
        while(m--)
        {
            scanf("%d%d",&i,&j);
            t[k]=j,p[k]=h[i],h[i]=k++;
            t[k]=i,p[k]=h[j],h[j]=k++;
            ++du[i],++du[j];
        }
        puts(tarjan()?"YES":"NO");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值