-
Notifications
You must be signed in to change notification settings - Fork 0
/
B1.cpp
43 lines (42 loc) · 809 Bytes
/
B1.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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector <int> g[maxn];
int a[maxn];
int ans;
bool dfs(int x, int par) {
bool done = false;
for(auto i : g[x]) {
if(i != par) {
bool var = dfs(i, x);
if(var == false) {
done = true;
swap(a[x], a[i]);
ans += 2;
}
}
}
return done;
}
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i < n; i++) {
int p, q;
scanf("%d %d", &p, &q);
g[p].push_back(q);
g[q].push_back(p);
}
for(int i = 1; i <= n; i++) a[i] = i;
bool var = dfs(1, 0);
if(!var) {
ans += 2;
swap(a[1], a[g[1].back()]);
}
printf("%d\n", ans);
for(int i = 1; i <= n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}