#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define all(a) (a).begin(), (a).end()
using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;
const int MXN = 1e5+5;
int n;
vi adj[MXN];
int deg[MXN];
int mn_ans[MXN], mx_ans[MXN];
ll mn_swap, mx_swap;
int sz[MXN], H[MXN];
void dfs(int u, int p) {
sz[u] = 1;
for(int v : adj[u]) if(v != p) {
dfs(v, u); sz[u] += sz[v];
}
}
int centroid(int u, int p) {
for(int v : adj[u]) if(v != p) {
if(2*sz[v] > n) return centroid(v, u);
}
return u;
}
void dfs1(int u, int p, vi &arr) {
H[u] = H[p]+1;
arr.PB(u);
for(int v : adj[u]) if(v != p) dfs1(v, u, arr);
}
int main() {
cin >> n;
for(int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].PB(b);
adj[b].PB(a);
deg[a]++, deg[b]++;
}
{
// min
queue<int> Q;
for(int i = 1; i <= n; i++) {
if(deg[i] == 1) {
Q.push(i);
}
}
iota(mn_ans+1, mn_ans+n+1, 1);
while(!Q.empty()) {
int i = Q.front(); Q.pop();
if(i != mn_ans[i]) continue;
deg[i] = 0;
bool found = false;
for(int j : adj[i]) {
if(deg[j] > 0) {
deg[j] = 0; found = true;
for(int t : adj[j]) {
if(deg[t] > 0) {
deg[t]--;
if(deg[t] == 1) {
Q.push(t);
}
}
}
swap(mn_ans[i], mn_ans[j]);
mn_swap += 2;
break;
}
}
if(!found) {
int j = adj[i].back();
swap(mn_ans[i], mn_ans[j]);
mn_swap += 2;
}
}
}
{
// max
dfs(1, 1);
int c = centroid(1, -1);
vi arr = {c};
for(int u : adj[c]) {
dfs1(u, c, arr);
}
for(int i = 1; i <= n; i++) {
mx_swap += 2*H[i];
}
for(int i = 0; i < n; i++) {
mx_ans[arr[i]] = arr[(i+(n+1)/2)%n];
}
}
cout << mn_swap << " " << mx_swap << "\n";
for(int i = 1; i <= n; i++) cout << mn_ans[i] << " \n"[i == n];
for(int i = 1; i <= n; i++) cout << mx_ans[i] << " \n"[i == n];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2664 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2660 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2580 KB |
Output is correct |
15 |
Correct |
2 ms |
2656 KB |
Output is correct |
16 |
Correct |
2 ms |
2660 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2648 KB |
Output is correct |
4 |
Correct |
3 ms |
2660 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2660 KB |
Output is correct |
7 |
Correct |
4 ms |
2792 KB |
Output is correct |
8 |
Correct |
3 ms |
2664 KB |
Output is correct |
9 |
Correct |
3 ms |
2664 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2664 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
4 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
3 ms |
2736 KB |
Output is correct |
19 |
Correct |
3 ms |
2772 KB |
Output is correct |
20 |
Correct |
3 ms |
2644 KB |
Output is correct |
21 |
Correct |
3 ms |
2660 KB |
Output is correct |
22 |
Correct |
4 ms |
2644 KB |
Output is correct |
23 |
Correct |
4 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2660 KB |
Output is correct |
25 |
Correct |
3 ms |
2660 KB |
Output is correct |
26 |
Correct |
3 ms |
2660 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
3 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2664 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2660 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2580 KB |
Output is correct |
15 |
Correct |
2 ms |
2656 KB |
Output is correct |
16 |
Correct |
2 ms |
2660 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2648 KB |
Output is correct |
21 |
Correct |
3 ms |
2660 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
3 ms |
2660 KB |
Output is correct |
24 |
Correct |
4 ms |
2792 KB |
Output is correct |
25 |
Correct |
3 ms |
2664 KB |
Output is correct |
26 |
Correct |
3 ms |
2664 KB |
Output is correct |
27 |
Correct |
3 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
3 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
3 ms |
2664 KB |
Output is correct |
32 |
Correct |
3 ms |
2644 KB |
Output is correct |
33 |
Correct |
4 ms |
2644 KB |
Output is correct |
34 |
Correct |
3 ms |
2644 KB |
Output is correct |
35 |
Correct |
3 ms |
2736 KB |
Output is correct |
36 |
Correct |
3 ms |
2772 KB |
Output is correct |
37 |
Correct |
3 ms |
2644 KB |
Output is correct |
38 |
Correct |
3 ms |
2660 KB |
Output is correct |
39 |
Correct |
4 ms |
2644 KB |
Output is correct |
40 |
Correct |
4 ms |
2668 KB |
Output is correct |
41 |
Correct |
2 ms |
2660 KB |
Output is correct |
42 |
Correct |
3 ms |
2660 KB |
Output is correct |
43 |
Correct |
3 ms |
2660 KB |
Output is correct |
44 |
Correct |
2 ms |
2644 KB |
Output is correct |
45 |
Correct |
3 ms |
2644 KB |
Output is correct |
46 |
Correct |
2 ms |
2644 KB |
Output is correct |
47 |
Correct |
3 ms |
2688 KB |
Output is correct |
48 |
Correct |
130 ms |
9696 KB |
Output is correct |
49 |
Correct |
114 ms |
10352 KB |
Output is correct |
50 |
Correct |
108 ms |
10348 KB |
Output is correct |
51 |
Correct |
104 ms |
8664 KB |
Output is correct |
52 |
Correct |
115 ms |
10244 KB |
Output is correct |
53 |
Correct |
90 ms |
9576 KB |
Output is correct |
54 |
Correct |
59 ms |
9992 KB |
Output is correct |
55 |
Correct |
136 ms |
17752 KB |
Output is correct |
56 |
Correct |
136 ms |
13704 KB |
Output is correct |
57 |
Correct |
127 ms |
12616 KB |
Output is correct |
58 |
Correct |
119 ms |
11428 KB |
Output is correct |
59 |
Correct |
130 ms |
10496 KB |
Output is correct |
60 |
Correct |
93 ms |
10788 KB |
Output is correct |
61 |
Correct |
98 ms |
10616 KB |
Output is correct |
62 |
Correct |
102 ms |
10676 KB |
Output is correct |
63 |
Correct |
94 ms |
10212 KB |
Output is correct |
64 |
Correct |
113 ms |
10872 KB |
Output is correct |
65 |
Correct |
97 ms |
10772 KB |
Output is correct |
66 |
Correct |
103 ms |
10152 KB |
Output is correct |
67 |
Correct |
78 ms |
8520 KB |
Output is correct |
68 |
Correct |
78 ms |
9496 KB |
Output is correct |
69 |
Correct |
98 ms |
10776 KB |
Output is correct |
70 |
Correct |
83 ms |
10208 KB |
Output is correct |
71 |
Correct |
76 ms |
8280 KB |
Output is correct |
72 |
Correct |
77 ms |
9092 KB |
Output is correct |
73 |
Correct |
98 ms |
10712 KB |
Output is correct |
74 |
Correct |
104 ms |
10120 KB |
Output is correct |
75 |
Correct |
120 ms |
10228 KB |
Output is correct |
76 |
Correct |
128 ms |
10236 KB |
Output is correct |
77 |
Correct |
100 ms |
10520 KB |
Output is correct |
78 |
Correct |
73 ms |
7980 KB |
Output is correct |
79 |
Correct |
75 ms |
8836 KB |
Output is correct |
80 |
Correct |
130 ms |
10240 KB |
Output is correct |
81 |
Correct |
89 ms |
10568 KB |
Output is correct |
82 |
Correct |
113 ms |
10536 KB |
Output is correct |