# HDU1272-小希迷宫

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

AC代码：

#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
using namespace std;
const int MAX = 100010;
const int INF = 0X3f3f3f;

int n, m;

int father[MAX];

bool vis[MAX];
void init() {//初始化！！！！！！！！一定注意  初始不要多。 我初始MAX大小  直接错
for(int i = 0; i <= 100000; i++) {
father[i] = i;
vis[i] = false;
}
}

int findfather(int x) {
int a = x;
while(x != father[x]) {
x = father[x];
}
while(a != father[a]) {
int z = a;
a = father[a];
father[z] = x;
}
return x;
}

void Union(int a, int b) {
int faA = findfather(a);
int faB = findfather(b);
if(faA > faB)
father[faA] = faB;
else
father[faB] = faA;
}

int main() {
int u, v;
while(scanf("%d %d", &u, &v) != EOF) {
if(u == -1 && v == -1)
break;
//        if(u == 0 && v == 0) {
//            cout << "Yes" << endl;
//            continue;
//          }
init();
int flag = 0;
while(1) {
if(u == 0 && v == 0)
break;
if(findfather(u) == findfather(v))//如果相等就判1
flag = 1;
Union(u, v);
vis[u] = true, vis[v] = true;
scanf("%d %d", &u, &v);
}
if(flag == 1)
printf("No\n");
else {
int sum = 0;
for(int i = 0; i <= 100000; i++) {// 这里也用题目中的最大值。！！！
if(vis[i] == true && father[i] == i)
sum++;
}
if(sum > 1)
printf("No\n");
else
printf("Yes\n");
}
}
return 0;
}