标签:com bottom tween src connect nbsp 题目 ++ main
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325
解题思路: 判断 连通 && 边数 == 节点数-1 && 只有一个根 即可。需要注意的是 0 0这种情况应该是 Yes
本题与Hdu1272神似
代码:
1 const int maxn = 1e5 + 5; 2 3 int fa[maxn], vis[maxn]; 4 int ind[maxn]; 5 6 void init(){ 7 for(int i = 1; i < maxn; i++) fa[i] = i; 8 memset(vis, 0, sizeof(vis)); 9 memset(ind, 0, sizeof(ind)); 10 } 11 int find(int x){ 12 return x == fa[x]? x: fa[x] = find(fa[x]); 13 } 14 void unite(int x, int y){ 15 x = find(x); 16 y = find(y); 17 if(x != y){ 18 fa[x] = y; 19 } 20 } 21 int main(){ 22 int a, b; 23 int tc = 1; 24 while(scanf("%d %d", &a, &b) && a >= 0){ 25 printf("Case %d ", tc++); 26 if(a == 0 && b == 0){ 27 puts("is a tree."); 28 continue; 29 } 30 int maxa = max(a, b), cnt = 1; 31 init(); 32 ind[b]++; 33 unite(a, b); 34 vis[a] = 1; vis[b] = 1; 35 while(scanf("%d %d", &a, &b) && a != 0 && b != 0){ 36 unite(a, b); 37 cnt++; 38 maxa = max(maxa, max(a, b)); 39 vis[a] = 1; vis[b] = 1; 40 ind[b]++; 41 } 42 int n = 0, la = 0, root = 0; 43 bool flag = true; 44 for(int i = 1; i <= maxa; i++){ 45 if(vis[i]) { 46 n++; 47 if(ind[i] == 0 && root == 0) { 48 root = i; 49 } 50 if(ind[i] == 0 && root != i){ 51 flag = false; 52 break; 53 } 54 if(la != 0 && find(i) != find(la)){ 55 flag = false; 56 break; 57 } 58 la = i; 59 } 60 } 61 // debug 62 // printf("-->flag:%d\ncnt:%d n:%d\nroot:%d\n", flag, cnt, n, root); 63 if(cnt == n - 1 && flag){ 64 puts("is a tree."); 65 } 66 else{ 67 puts("is not a tree."); 68 } 69 } 70 }
题目:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25706 Accepted Submission(s): 5867
标签:com bottom tween src connect nbsp 题目 ++ main
原文地址:http://www.cnblogs.com/bolderic/p/7294440.html