zoukankan      html  css  js  c++  java
  • POJ 3713 Transferring Sylla (三联通分量)

    Transferring Sylla
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 3533   Accepted: 988

    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


    思路:判断给定图是否为三联通分量;
        以下情况不是:
        1.图不连通
        2.图有割点
        3.删去一个点后存在割点。
       这题我用了读入挂(抄的别人的板子)才过。。。。
    代码:
      1 #include<iostream>
      2 #include<algorithm>
      3 #include<vector>
      4 #include<stack>
      5 #include<queue>
      6 #include<map>
      7 #include<set>
      8 #include<cstdio>
      9 #include<cstring>
     10 #include<cmath>
     11 #include<ctime>
     12 #define fuck(x) cout<<#x<<" = "<<x<<endl;
     13 #define ls (t<<1)
     14 #define rs ((t<<1)+1)
     15 using namespace std;
     16 typedef long long ll;
     17 typedef unsigned long long ull;
     18 const int maxn = 50086;
     19 const int inf = 2.1e9;
     20 const ll Inf = 999999999999999999;
     21 const int mod = 1000000007;
     22 const double eps = 1e-6;
     23 const double pi = acos(-1);
     24 int f[1000];
     25 int index,root,n,m,DEL;
     26 int cnt,Head[1000],v[maxn],Next[maxn];
     27 void init()
     28 {
     29     cnt=0;root=1;
     30     memset(Head,-1,sizeof(Head));
     31     for(int i=0;i<=n;i++){
     32         f[i]=i;
     33     }
     34 }
     35 
     36 inline bool scan_d(int &num)
     37 {
     38         char in;bool IsN=false;
     39         in=getchar();
     40         if(in==EOF) return false;
     41         while(in!='-'&&(in<'0'||in>'9')) in=getchar();
     42         if(in=='-'){ IsN=true;num=0;}
     43         else num=in-'0';
     44         while(in=getchar(),in>='0'&&in<='9'){
     45                 num*=10,num+=in-'0';
     46         }
     47         if(IsN) num=-num;
     48         return true;
     49 }
     50 void add(int x,int y)
     51 {
     52     cnt++;
     53     v[cnt]=y;
     54     Next[cnt]=Head[x];
     55     Head[x]=cnt;
     56 
     57     cnt++;
     58     v[cnt]=x;
     59     Next[cnt]=Head[y];
     60     Head[y]=cnt;
     61 }
     62 
     63 int num[508],low[508];
     64 bool dfs(int cur,int father)
     65 {
     66     int child=0;
     67     index++;
     68     num[cur]=index;
     69     low[cur]=index;
     70     for(int k=Head[cur];k!=-1;k=Next[k]){
     71         if(v[k]==DEL){continue;}
     72 //        fuck(v[k])
     73         if(num[v[k]]==0){
     74             child++;
     75             if(!dfs(v[k],cur)){return false;}
     76             low[cur]=min(low[cur],low[v[k]]);
     77             if(cur!=root&&low[v[k]]>=num[cur]){
     78                 return false;
     79             }
     80             if(cur==root&&child==2){
     81                 return false;
     82             }
     83         }
     84         else if(v[k]!=father){
     85             low[cur]=min(low[cur],num[v[k]]);
     86         }
     87     }
     88     return true;
     89 }
     90 
     91 bool solve()
     92 {
     93     memset(num,0,sizeof(num));
     94     memset(low,0,sizeof(low));
     95     root=2;
     96     DEL=1;
     97     if(!dfs(2,root)){return false;}
     98     root=1;
     99     for(int i=2;i<=n;i++){
    100         memset(num,0,sizeof(num));
    101         memset(low,0,sizeof(low));
    102         DEL=i;
    103         if(!dfs(1,root)){
    104 //            cout<<i<<endl;
    105             return false;
    106         }
    107     }
    108     return true;
    109 }
    110 
    111 int getf(int x)
    112 {
    113     if(f[x]==x){return x;}
    114     return f[x]=getf(f[x])
    115 ;}
    116 
    117 bool Merge(int x,int y)
    118 {
    119     int s1=getf(x);
    120     int s2=getf(y);
    121     if(s1!=s2){
    122         f[s1]=s2;
    123         return true;
    124     }
    125     return false;
    126 }
    127 
    128 int main()
    129 {
    130 //    ios::sync_with_stdio(false);
    131 //    freopen("in.txt","r",stdin);
    132 
    133     while(scanf("%d%d",&n,&m)!=EOF&&n&&m){
    134         init();
    135         int snum = 0;
    136         for(int i=1;i<=m;i++){
    137             int x,y;
    138             scan_d(x);scan_d(y);
    139             add(x+1,y+1);
    140             if(Merge(x+1,y+1)){
    141                 snum++;
    142             }
    143         }
    144         if(snum!=n-1){printf("NO
    ");continue;}
    145         memset(num,0,sizeof(num));
    146         memset(low,0,sizeof(low));
    147         root=1;
    148         DEL=0;
    149         if(!dfs(1,root)){printf("NO
    ");continue;}
    150 
    151 
    152         if(solve()){printf("YES
    ");}
    153         else printf("NO
    ");
    154 
    155     }
    156 
    157     return 0;
    158 }
    View Code
  • 相关阅读:
    Taglib笔记摘自http://tech.acnow.net/Html/Program/Java/200204/10/092611588.shtml
    Subversion安装手记
    java 数组排序
    JAVA 反编译
    开发EJB
    HTML 4.0 语 法 教 学 转载http://chinese.allproducts.com.tw/GB/html/#t1
    javaCollectionslist
    Ruby on Rack #1 与Rack的第一次亲密接触
    Ubuntu 安装 ruby on rails [转]来源:Ubuntu社区 作者:承古韵
    收集整理的对#!bin/sh的认识
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/10293480.html
Copyright © 2011-2022 走看看