forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10054.cpp
116 lines (104 loc) · 1.49 KB
/
10054.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 51;
bool visited[MAXN];
int n, adj[MAXN][MAXN], degree[MAXN];
deque<int> path;
void visit_component(int u)
{
if (visited[u])
{
return;
}
visited[u] = true;
for (int v = 0; v < n; ++v)
{
if (adj[u][v])
{
visit_component(v);
}
}
}
void euler(int u)
{
for (int v = 0; v < n; ++v)
{
if (adj[u][v])
{
adj[u][v]--;
adj[v][u]--;
euler(v);
}
}
path.push_front(u);
}
int main()
{
int T;
scanf("%d", &T);
for (int t = 1; t <= T; ++t)
{
if (t > 1)
{
printf("\n");
}
printf("Case #%d\n", t);
bzero(visited, sizeof visited);
bzero(adj, sizeof adj);
bzero(degree, sizeof degree);
path.clear();
n = 0;
int edges;
for (scanf("%d", &edges); edges--;)
{
int u, v;
scanf("%d %d", &u, &v);
n = max(n, u + 1);
n = max(n, v + 1);
degree[u]++;
degree[v]++;
adj[u][v]++;
adj[v][u]++;
}
for (int i = 0; i < n; ++i)
{
if (degree[i] > 0)
{
visit_component(i);
break;
}
}
bool impossible = false;
for (int i = 0; i < n; ++i)
{
if (degree[i] > 0 && !visited[i] || degree[i] % 2 == 1)
{
impossible = true;
break;
}
}
if (!impossible)
{
for (int i = 0; i < n; ++i)
{
if (degree[i] > 0)
{
euler(i);
break;
}
}
}
if (impossible)
{
printf("some beads may be lost\n");
}
else
{
for (int i = 0; i < path.size() - 1; ++i)
{
printf("%d %d\n", path[i], path[i + 1]);
}
}
}
return 0;
}