标签:hdoj1272
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
Yes Yes No
并查集若检测出环则直接NO,否则还要判断是否是森林,若是则no;
#include <stdio.h> #include <string.h> int pre[100002]; bool flag[100002]; int find(int k){ if(pre[k] == 0) return k; int i = k; while(pre[i] != 0) i = pre[i]; int j = k, t; while(pre[j] != 0){ t = pre[j]; pre[j] = i; j = t; } return i; } int main(){ int a, b, ok = 1, x, y, mark; while(scanf("%d%d", &a, &b)){ if(a == -1 && b == -1) break; if(a == 0 && b == 0){ if(ok == 0) printf("No\n"); else{ mark = find(mark); for(int i = 1; i < 100001; ++i){ if(flag[i] && mark != find(i)){ ok = 0; break; } } printf(ok ? "Yes\n" : "No\n"); } ok = 1; memset(pre, 0, sizeof(pre)); memset(flag, 0, sizeof(flag)); continue; } if(ok == 0) continue; mark = x = find(a); y = find(b); flag[a] = flag[b] = 1; if(x == y) ok = 0; else mark = pre[x] = y; } return 0; }
标签:hdoj1272
原文地址:http://blog.csdn.net/chang_mu/article/details/24739337