Uva--10596 (欧拉回路)
2014-07-09 01:15:50
题意&思路:裸欧拉回路,竟然被r=0坑了。。。(注意下:限制条件能强则强)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 6 int a,b,n,r,deg[205],fa[205]; 7 8 int Find(int x){ 9 if(fa[x] != x) return Find(fa[x]); 10 return fa[x]; 11 } 12 13 void Union(int a,int b){ 14 int x = Find(a); 15 int y = Find(b); 16 if(x != y) fa[y] = x; 17 } 18 19 int main(){ 20 while(scanf("%d %d",&n,&r) == 2){ 21 for(int i = 0; i < n; ++i) 22 deg[i] = 0,fa[i] = i; 23 for(int i = 0; i < r; ++i){ 24 scanf("%d %d",&a,&b); 25 Union(a,b); 26 deg[a]++; 27 deg[b]++; 28 } 29 int setn = 0; 30 for(int i = 0; i < n; ++i) 31 if(deg[i] && fa[i] == i) ++setn; 32 if(setn != 1){ //需要这个强限制条件来排除r == 0的情况 33 printf("Not Possible\n"); 34 continue; 35 } 36 int flag = 1; 37 for(int i = 0; i < n; ++i) 38 if(deg[i] && deg[i] % 2){ 39 flag = 0; 40 break; 41 } 42 if(flag) printf("Possible\n"); 43 else printf("Not Possible\n"); 44 } 45 return 0; 46 }