分析
- 迷宫可能为空,即该组数据仅有 0 0
- 每组数据要读完
代码
#include<bits/stdc++.h>
using namespace std;
#define MXN 100010
int N, M, fa[MXN], flag;
int find(int x){
if(x != fa[x]) fa[x] = find(fa[x]);
return fa[x];
}
void merge(int x, int y){
int t1 = find(x);
int t2 = find(y);
if(t1 == t2) flag = 1;
else fa[t1] = t2;
}
int main(){
int a, b;
flag = 0;
memset(fa, -1, sizeof fa);
while(scanf("%d %d", &a, &b)){
if(a == -1 && b == -1) break;
if(a == 0 && b == 0){
if(flag == 1) printf("No\n");
else{
fa[0] = 0;
for(int i = 1; i < MXN; i++) if(fa[i] == i) fa[0]++;
printf("%s\n", fa[0] > 1 ? "No" : "Yes");
}
flag = 0;
memset(fa, -1, sizeof fa);
continue;
}
if(flag == 1) continue;
if(fa[a] == -1) fa[a] = a;
if(fa[b] == -1) fa[b] = b;
merge(a, b);
}
return 0;
}