Uva--10054 (欧拉回路)

2014-07-08 23:40:29

题意&思路:无向图欧拉回路。

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 int Case,n,a,b,vis[55][55],g[55][55],deg[55];
 8 void Euler(int u){
 9     for(int v = 1; v <= 50; ++v){
10         if(g[u][v]){
11             g[u][v]--;
12             g[v][u]--;
13             Euler(v);
14             printf("%d %d\n",v,u);
15         }
16     }
17 }
18 int main(){
19     scanf("%d",&Case);
20     for(int t = 1; t <= Case; ++t){
21         scanf("%d",&n);
22         memset(deg,0,sizeof(deg));
23         memset(g,0,sizeof(g));
24         for(int i = 0; i < n; ++i){
25             scanf("%d %d",&a,&b); //1 to 50
26             g[a][b]++;
27             g[b][a]++;
28             deg[a]++;
29             deg[b]++;
30         }
31         if(t != 1) puts("");
32         printf("Case #%d\n",t);
33         int flag = 1;
34         for(int i = 1; i <= 50; ++i)
35             if(deg[i] && deg[i] % 2){
36                 flag = 0;
37                 break;
38             }
39         if(!flag)
40             printf("some beads may be lost\n");
41         else{
42             for(int i = 1; i <= 50; ++i)
43                 if(deg[i]){
44                     Euler(i);
45                     break;
46                 }
47         }
48     }
49     return 0;
50 }

 

posted @ 2014-07-08 23:43  Naturain  阅读(111)  评论(0编辑  收藏  举报