标签:
1今天准备复习三道以前做过的题呢,结果只看了一道,也因为交题的时候没把测试时候为了方便而改的数字改过来而wrong answer,浪费了好长时间,啊啊啊~~
不过这道题应该是掌握了,嘿嘿……
Description
Input
Output
Sample Input
Sample Output
#include <cstdio> #include <algorithm> using namespace std; int arr[100010], mark[100010], flag; int findx(int x) { int r = x; while(arr[r] != r) r = arr[r]; return r; } void fix(int a, int b) { int fx = findx(a); int fy = findx(b); if(fx != fy) arr[fx] = fy; else flag = 0; } int main() { int a, b, cnt; while(scanf("%d%d", &a, &b) && (a != -1 && b != -1)) { flag = 1, cnt = 0; if(a == 0 && b == 0) { printf("Yes\n"); continue; } for(int i = 0; i < 100010; i++) { arr[i] = i; mark[i] = 0; } //int mi = 100010, ma = -1; while(a || b) { /* if(ma < max(a, b)) ma = max(a, b); if(mi > min(a, b)); mi = min(a, b);*/ fix(a, b); mark[a] = mark[b] = 1; scanf("%d%d", &a, &b); } for(int i = 1; i <= 100000; i++) { if(arr[i] == i && mark[i]) cnt++; if(cnt > 1) { flag = 0; break; } } if(flag == 1) printf("Yes\n"); else printf("No\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/rain-1/p/4787752.html