http://acm.hdu.edu.cn/showproblem.php?pid=1272
题意:中文题辣。。。
思路:并查集,需要处理森林。。。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int pre[100005];
bool vis[100005];
int sta[100005], top;
int Init(){
memset(vis, false, sizeof(vis));
for(int i = 0; i < 100005; i++){
pre[i] = i;
}
return 0;
}
int Find(int rt)
{
int top = 0;
while(pre[rt] != rt){
sta[top++] = rt;
rt = pre[rt];
}
while(top){
pre[sta[--top]] = rt;
}
return rt;
}
int Union(int x, int y)
{
if((x ^ y) & 1)
pre[x] = y;
else
pre[y] = x;
return 0;
}
int main()
{
int x, y, rt;
while(~scanf("%d%d", &x, &y) && ( x != -1 || y != -1)){
Init();
rt = x;
pre[y] = rt;
bool flag = true;
vis[x] = vis[y] = true;
int maxn = x > y ? x : y;
while((x || y) && scanf("%d%d", &x, &y) && (x || y)){
if(flag){
vis[x] = vis[y] = true;
maxn = x > maxn ? x : maxn;
maxn = y > maxn ? y : maxn;
x = Find(x);
y = Find(y);
}
if(x == y)
flag = false;
if(flag){
Union(x, y);
}
}
int cnt = 0;
for(int i = 1; i <= maxn && flag; i++){
if(!vis[i])
continue;
if(pre[i] == i){
cnt++;
if(cnt >= 2){
flag = false;
break;
}
}
}
if(flag){
printf("Yes\n");
}
else{
printf("No\n");
}
}
return 0;
}