标签:
http://acm.hdu.edu.cn/showproblem.php?pid=1272
分析:和Poj1308一模一样啊(实质上都是判断是不是一棵树),就除了最后的结论的写法不一样。照搬那题的代码一次AC。。。
判断树:
1.空树也是树
2.森林不符合题意,只能是一棵树
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #define maxn 110000 #define oo 0xfffffff using namespace std; int father[maxn],v[maxn]; int flag; int Find(int x) { while(x!=father[x]) x=father[x]; return x; } void solve(int a, int b) { v[a] = v[b] = 1; if(a==b) { flag=1; } else { int ra = Find(a); int rb = Find(b); if(ra!=rb) father[rb]=ra; else flag = 1; } } int main() { int a, b, fist; while(scanf("%d %d", &a, &b), a!=-1 || b!=-1) { if(a==0 && b==0)///空树也是树 { printf("Yes\n"); continue; } flag = 0; fist = a; memset(v, 0, sizeof(v)); for(int i=1; i<maxn; i++) father[i] = i; solve(a, b); while(scanf("%d %d", &a, &b),a+b) { solve(a, b); } for(int i=1; i<maxn; i++) { if(Find(i)!=Find(fist) && v[i]) { flag = 1; break; } } if(flag) printf("No\n"); else printf("Yes\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/daydayupacm/p/5726293.html