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
要注重如何形成树。树的条件是顶点-1 = 边。森林就不是这样了。
这道题目的问题就是在于有没有注重a == 0和b == 0.如果刚开始就是输入a==0&&b==0那就要输出YES。
剩下的讨论就是,能否形成树,然后加上那条边会不会形成圈。就好了
#include<cstring> #include<cstdio> #include<algorithm> using namespace std; const int inf = 100000 + 20; bool used[inf]; int par[inf], level[inf]; int a, b, res, sum, al; int find(int x){ if(par[x] == x)return x; return par[x] = find(par[x]); } void unite(int x, int y){ x = find(x); y = find(y); if(x == y)return ; if(level[x] > level[y]){ par[y] = x; } else { par[x] = y; if(level[x] == level[y])level[x]++; } } bool same(int x, int y){ return find(x) == find(y); } int main(){ while(scanf("%d%d", &a, &b)!=EOF){ if(a == -1 && b == -1)break; if(a==0&&b==0)//如果没有这句话 就是WA { printf("Yes\n"); continue; } memset(level, 0, sizeof(level)); for(int i = 1; i <= inf; i++){ par[i] = i; } res = sum = 0; int v = 1;//判断是否会形成圈 memset(used, 0, sizeof(used)); while(a && b){ if(!used[a]){ used[a] = 1; res++; } if(!used[b]){ used[b] = 1; res++; } if(!same(a, b) ){ unite(a, b); sum++; } else if(a != b){ v = 0; } al++; scanf("%d%d", &a, &b); } // printf("sum = %d\n", sum); // printf("res = %d\n", res); if(sum + 1 == res && v == 1)printf("Yes\n"); else printf("No\n"); } return 0; }