hdu 1272 小希的迷宮

http://acm.split.hdu.edu.cn/showproblem.php?pid=1272


上次Gardon的迷宮城堡小希玩了很久(見Problem B),現在她也想設計一個迷宮讓Gardon來走。但是她設計迷宮的思路不一樣,首先她認爲所有的通道都應該是雙向連通的,就是說如果有一個通道連通了房間A和B,那麼既可以通過它從房間A走到房間B,也可以通過它從房間B走到房間A,爲了提高難度,小希希望任意兩個房間有且僅有一條路徑可以相通(除非走了回頭路)。小希現在把她的設計圖給你,讓你幫忙判斷她的設計圖是否符合她的設計思路。比如下面的例子,前兩個是符合條件的,但是最後一個卻有兩種方法從5到達8。

 

Input
輸入包含多組數據,每組數據是一個以0 0結尾的整數對列表,表示了一條通道連接的兩個房間的編號。房間的編號至少爲1,且不超過100000。每兩組數據之間有一個空行。
整個文件以兩個-1結尾。
 

Output
對於輸入的每一組數據,輸出僅包括一行。如果該迷宮符合小希的思路,那麼輸出"Yes",否則輸出"No"。
 

Sample Input
6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
 

Sample Output
Yes Yes No
 

Author
Gardon
 



#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int inf = 0x7fffffff;
#define ms(x) memset((x),0,sizeof(x))

const int Max = 100001;
int fa[Max];
int vfa[Max];
bool flag = false;
int fi(int x){
    return fa[x]==x?x:fa[x]=fi( fa[x] );
}
void Lian(int x,int  y){
    int p1=fi(x),p2=fi(y);
    if( p1 == p2 ) { flag = true; return;}
    fa[p1]=p2;
}
void init(){
    for(int i = 0;i < Max;i++) fa[i] = i;
    ms(vfa); flag = false;
}
int main(){
 //   freopen("1.txt","r",stdin);
    int a,b;
    init();
    while(scanf("%d%d",&a,&b)){
        if( a==-1&&b==-1 ) break;
        else if( a==0 && b==0 ){
            if( flag ) puts("No");
            else{
                int cnt = 0;
                for(int i = 1;i < Max;i++){
                    if( vfa[i] && fa[i] == i ) cnt++;
                }
                if( cnt > 1 ) puts("No");
                else puts("Yes");
            }
            init();
        }
        else{
            Lian(a,b);
            vfa[a]=vfa[b]=1;
        }
    }
    return 0;
}



void BingChaJI(){
    int fi(int x){
        return fa[x]==x?x:fa[x]=fi( fa[x] );
    }
    void Lian(int x,int  y){
        int p1=fi(x),p2=fi(y);
        if( p1 == p2 ) return;
        fa[p1]=p2;
    }
    int check(int x,int y){
        int p1=fi(x),p2=fi(y);
        if( p1 == p2 ) return 1;
        return 0;
    }
    void init(){
        for(int i = 0;i < maxn;i++) fa[i] = i;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章