标签:
Description
Input
Output
Sample Input
Sample Output
#include <stdio.h> #define max_num 100000+10 typedef struct { int vis, root, conn; }Node; Node node[max_num]; void init() { for(int i = 0; i < max_num; i++) { node[i].vis = 0; node[i].root = i; node[i].conn = 0; } } int find_root(int x) { while(x != node[x].root) x = node[x].root; return x; } void union_set(int x, int y) { x = find_root(x); y = find_root(y); if(x != y) node[y].root = x; } int main() { int n, m; bool flag = true; int case_num = 1; init(); //freopen("input.txt", "r", stdin); while(scanf("%d%d", &n, &m), n >= 0 && m >= 0) { if(!flag && (n != 0 && m != 0)) continue; if(n == 0 && m == 0) { int root_num = 0; for(int i = 1; i < max_num; i++) { if(find_root(i) == i && node[i].vis) root_num++; if(node[i].conn > 1) flag = false; } if(root_num > 1) flag = false; if(flag) printf("Case %d is a tree.\n", case_num++); else printf("Case %d is not a tree.\n", case_num++); flag = true; init(); continue; } if(n != m && find_root(n) == find_root(m)) flag = false; else { node[n].vis = 1; node[m].vis = 1; node[m].conn++; union_set(n, m); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/rain-1/p/4878364.html