标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10267 Accepted Submission(s): 3749
并查集,注意:欧拉回路是联通图并且各个顶点度数都为偶
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 int n,m; 8 int d[1001]; 9 int pre[1001]; 10 11 int find(int x) 12 { 13 if(pre[x] == 0) 14 return x; 15 int ans = find(pre[x]); 16 pre[x]=ans; 17 return ans; 18 } 19 20 void unite(int a, int b) 21 { 22 int x = find(a); 23 int y = find(b); 24 if(x != y) 25 { 26 pre[x] = y; 27 } 28 } 29 30 int main() 31 { 32 int tmp1, tmp2, count = 0; 33 bool success = true; 34 while(scanf("%d", &n) && n != 0) 35 { 36 memset(d, 0, sizeof(d)); 37 memset(pre, 0, sizeof(pre)); 38 count = 0; 39 scanf("%d", &m); 40 success = true; 41 for(int i = 1; i <= m; i++) 42 { 43 scanf("%d %d", &tmp1, &tmp2); 44 unite(tmp1, tmp2); 45 d[tmp1]++; 46 d[tmp2]++; 47 } 48 for(int i = 1; i <= n; i++) 49 { 50 if(d[i] % 2 != 0) 51 { 52 cout << 0 << endl; 53 success = false; 54 break; 55 } 56 } 57 if(!success) 58 { 59 continue; 60 } 61 for(int i = 1; i <= n; i++) 62 { 63 if(pre[i] == 0) 64 { 65 count++; 66 } 67 } 68 if(count > 1) 69 { 70 cout << 0 << endl; 71 } 72 else 73 { 74 cout << 1 << endl; 75 } 76 } 77 return 0; 78 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/4558353.html