标签:
http://poj.org/problem?id=1308
It A Tree?
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 25470 | Accepted: 8713 |
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
Case 1 is a tree. Case 2 is a tree. Case 3 is not a tree.
与hdu1272做法相同
不需要用并查集,统计点的个数和线的条数,如果 点的个数 - 线的条数 = 1,则满足
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #define N 100005 int main() { int a, b, k, m, i, x = 0; bool vis[N]; while(~scanf("%d%d", &a, &b)) { k = m = 0; x++; memset(vis, false, sizeof(vis)); if(a == -1 && b == -1) break; if(a == 0 && b == 0) { printf("Case %d", x); printf(" is a tree.\n"); continue; } else { vis[a] = vis[b] = true; k++; } while(~scanf("%d%d", &a, &b)) { if(a == 0 && b == 0) break; vis[a] = vis[b] = true; k++; } for(i = 0 ; i < N ; i++) if(vis[i]) m++; printf("Case %d", x); if(m - k == 1) printf(" is a tree.\n"); else printf(" is not a tree.\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/qq2424260747/p/4677244.html