UVa 10054 (打印欧拉回路) The Necklace

将每个颜色看成一个顶点,对于每个珠子在两个颜色之间连一条无向边,然后求欧拉回路。

 1 #include <cstdio>

 2 #include <cstring>

 3 

 4 const int maxn = 50 + 5;

 5 int G[maxn][maxn], deg[maxn];

 6 

 7 void Euler(int u)

 8 {

 9     for(int v = 1; v <= 50; v++) if(G[u][v])

10     {

11         G[u][v]--; G[v][u]--;

12         Euler(v);

13         printf("%d %d\n", v, u);

14     }

15 }

16 

17 int main()

18 {

19     //freopen("in.txt", "r", stdin);

20 

21     int T; scanf("%d", &T);

22     for(int kase = 1; kase <= T; kase++)

23     {

24         memset(G, 0, sizeof(G));

25         memset(deg, 0, sizeof(deg));

26 

27         if(kase > 1) puts("");

28         printf("Case #%d\n", kase);

29 

30         int n, a, b, st;

31         scanf("%d", &n);

32         for(int i = 0; i < n; i++)

33         {

34             scanf("%d%d", &a, &b);

35             st = a;

36             deg[a]++; deg[b]++;

37             G[a][b]++; G[b][a]++;

38         }

39 

40         bool ok = true;

41         for(int i = 1; i <= 50; i++) if(deg[i] % 2) { ok = false; break; }

42         if(!ok) { puts("some beads may be lost"); continue; }

43 

44         Euler(st);

45 

46     }

47 

48     return 0;

49 }
代码君

 

你可能感兴趣的:(uva)