标签:思路 c++ init scanf 通过 otto scan bsp 空行
http://acm.hdu.edu.cn/showproblem.php?pid=1272
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 +10; int f[maxn], A[maxn]; int N, M; int cnt = 0; void init() { for(int i = 1; i <= maxn; i ++) f[i] = i; } int Find(int x) { if(f[x] != x) f[x] = Find(f[x]); return f[x]; } int Merge(int x, int y) { int fx = Find(x); int fy = Find(y); if(fx != fy) { f[fx] = fy; return 1; } else return 0; } int main() { while(true) { init(); int num = 0; bool flag = true; while(~scanf("%d%d", &N, &M)) { if(N == -1 && M == -1) return 0; else if(!N && !M) break; else if(flag) { if(!Merge(N, M)) flag = false; A[++ num] = N; A[++ num] = M; } } if(flag) { for(int j = 1; j <= num; j ++) { if(Find(f[A[j]]) != Find(f[A[1]])) flag = false; } if(flag) printf("Yes\n"); else printf("No\n"); } else printf("No\n"); } return 0; }
标签:思路 c++ init scanf 通过 otto scan bsp 空行
原文地址:https://www.cnblogs.com/zlrrrr/p/9720450.html