-
Notifications
You must be signed in to change notification settings - Fork 2
/
10054.cpp
123 lines (111 loc) · 1.73 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
117
118
119
120
121
122
123
#include<stdio.h>
int B, Max, In, Ed;
int Dg[60], st[2100], rank[60], pp[60];
int link[60][60];
char F[60];
struct ss {
int u, v;
}ed[1002];
void Set(int min) {
int i;
for(i = min; i<= Max; i++) {
rank[i] = 0;
pp[i] = i;
}
}
int Find(int n) {
if(pp[n] != n)
pp[n] = Find(pp[n]);
return pp[n];
}
void Link(int x, int y) {
if(rank[x]>rank[y])
pp[y] = x;
else {
pp[x] = y;
if(rank[y]>rank[x])
rank[y]++;
}
}
void Recur(int n, int s) {
int i;
for(i = s; i<= Max; i++) {
if(link[n][i]) {
link[n][i]--;
link[i][n]--;
Recur(i,s);
}
}
st[In++] = n;
}
int IsConnected(int min) {
int i, tn = 0, x, y;
Set(min);
for(i = min; i<= Max; i++) {
if(F[i]) tn++;
}
for(i = 0; i<Ed; i++) {
x = Find(ed[i].u);
y = Find(ed[i].v);
if(x != y){
Link(x,y);
tn --;
}
}
if(tn > 1) return 0;
return 1;
}
void Cal(int min) {
int i;
for(i = min; i<= Max; i++) {
if(Dg[i] % 2) {
printf("some beads may be lost\n");
return;
}
}
if(!IsConnected(min)) {
printf("some beads may be lost\n");
return;
}
In = 0;
Recur(min,min);
for(i = 0; i<In-1; i++) printf("%d %d\n",st[i],st[i+1]);
}
void Free(int min) {
int i, j;
for(i = min; i<= Max; i++) {
Dg[i] = 0;
F[i] = 0;
for(j = min; j<= Max; j++)
link[i][j] = 0;
}
}
int main() {
int i, u, v, min;
int kase, k = 0;
scanf("%d",&kase);
while(kase--) {
Max = Ed = 0;
min = 100;
scanf("%d",&B);
for(i = 0; i<B; i++) {
scanf("%d%d",&u,&v);
ed[Ed].u = u;
ed[Ed++].v = v;
F[u] = F[v] = 1;
if(u>Max) Max = u;
if(v>Max) Max = v;
if(min>u) min = u;
if(min>v) min = v;
link[u][v] ++;
link[v][u] ++;
Dg[u]++;
Dg[v]++;
}
if(k++>0) printf("\n");
printf("Case #%d\n",k);
Cal(min);
Free(min);
}
return 0;
}