还是并查集判断环的问题。。。这个题输入弄的很恶心。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int int_max = 0x07777777;
const int int_min = 0x80000000;
int root[110000];
int vis[110000];
int x,y;
int find (int a){
while(root[a]!=a){
a = root[a];
}
return a;
}
void combine (int a, int b){
int aa = find(a);
int bb = find(b);
if(aa!=bb){
root[bb] = aa;
}
}
int main(int argc, const char * argv[])
{
while(scanf("%d %d", &x, &y)!=EOF){
if(x==-1 && y==-1) break;
if(x==0 && y==0){
cout << "Yes" << endl;
continue;
}
int result= 0;
int maxn = 0;
for(int i = 0; i < 110000; i++) root[i] = i;
memset(vis, 0, sizeof(vis));
while(x||y){
maxn = (maxn < (x < y ? y : x) ? (x < y ? y : x) : maxn);
vis[x] = 1;
vis[y] = 1;
if(find(x)==find(y)) result = 1;
else combine(x, y);
cin >> x >> y;
}
int count = 0;
for(int i = 1; i <= maxn; i++){
if(vis[i] && root[i]==i) count++;
}
if(result==1) cout << "No" << endl;
else {
if(count!=1) cout << "No" << endl;
else cout << "Yes" << endl;
}
}
}