USACO Riding the Fences

Code

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
/*
ID: wcr19961
PROG: fence
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 510;
int d[maxn];
int g[maxn][maxn];
int odd[5], cnt;
stack<int> s;
void dfs(int u) {
for (int i = 1; i < maxn; ++i) {
if (g[u][i] > 0) {
--g[u][i], --g[i][u];
dfs(i);
s.push(i);
}
}
}
int main() {
freopen("fence.in", "r", stdin);
freopen("fence.out", "w", stdout);
int F;
scanf("%d", &F);
while (F--) {
int u, v;
scanf("%d%d", &u, &v);
++d[u], ++d[v];
++g[u][v], ++g[v][u];
}
int st = -1;
bool first = true;
for (int i = 1; i < maxn; ++i) {
if (d[i] & 1) {
odd[++cnt] = i;
}
if (d[i] > 0 && first) {
st = i;
first = false;
}
}
if (cnt == 2) {
st = odd[1];
}
dfs(st);
s.push(st);
while (!s.empty()) {
printf("%d\n", s.top());
s.pop();
}
return 0;
}

支付宝扫码领红包