# (并查集 不太会) HDU 1272 小希的迷宫

Problem Description

Input

Output

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

C++代码：
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 100010;
int father[maxn],vis[maxn];
int flag = 0;
int Find(int x){
while(x != father[x]){
//        father[x] = father[father[x]];
x = father[x];
}
return x;
}
void Union(int a,int b){
int ax = Find(a);
int bx = Find(b);
if(ax != bx)
father[ax] = bx;
else
flag = 0;
}
int main(){
int a,b;
while(cin>>a>>b){
if(a == -1 && b == -1)
break;
if(a == 0 && b == 0){
cout<<"Yes"<<endl;
continue;
}
for(int i = 1; i < maxn; i++){
father[i] = i;
vis[i] = 0;
}
vis[a] = vis[b] = 1;
flag = 1;
Union(a,b);
while(cin>>a>>b){
if(a == 0 && b == 0) break;
vis[a] = vis[b] = 1;
Union(a,b);
}
int sum = 0;
for(int i = 1; i < maxn; i++){
if(vis[i] && father[i] == i)
sum++;

if(sum > 1){
flag = 0;
break;
}
}
if(flag == 1) puts("Yes");
else puts("No");
}
return 0;
}

posted @ 2019-05-22 21:03  PJCK  阅读(99)  评论(0编辑  收藏  举报