#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MAX_L = __lg(MAX_N) + 1;
int up[MAX_L][MAX_N];
vector<int> G[MAX_N];
int depth[MAX_N], small[MAX_N], large[MAX_N];
vector<int> ord;
void DFS(int u, int p) {
depth[u] = (u == 0 ? 0 : depth[p] + 1);
ord.push_back(u);
up[0][u] = p;
for (int i = 0; i < MAX_L - 1; i++) {
up[i + 1][u] = up[i][up[i][u]];
}
for (int v : G[u]) {
if (v != p) {
DFS(v, u);
}
}
if (small[u] == u) {
swap(small[u], small[u == 0 ? G[u][0] : p]);
}
}
int lca(int u, int v) {
if (depth[u] < depth[v]) {
swap(u, v);
}
for (int i = MAX_L - 1; i >= 0; i--) {
if (depth[u] - depth[v] >= (1 << i)) {
u = up[i][u];
}
}
if (u == v) {
return u;
}
for (int i = MAX_L - 1; i >= 0; i--) {
if (up[i][u] != up[i][v]) {
u = up[i][u];
v = up[i][v];
}
}
return up[0][u];
}
int distance(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
G[u].push_back(v);
G[v].push_back(u);
}
iota(small, small + N, 0);
DFS(0, 0);
for (int i = 0; i < N; i++) {
large[ord[i]] = ord[(i + N / 2) % N];
}
int64_t smallest = 0, largest = 0;
for (int i = 0; i < N; i++) {
smallest += distance(i, small[i]);
largest += distance(i, large[i]);
}
cout << smallest << " " << largest << "\n";
for (int i = 0; i < N; i++) {
cout << 1 + small[i] << " \n"[i + 1 == N];
}
for (int i = 0; i < N; i++) {
cout << 1 + large[i] << " \n"[i + 1 == N];
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
1 ms |
2700 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2680 KB |
Output is correct |
11 |
Correct |
2 ms |
2680 KB |
Output is correct |
12 |
Correct |
1 ms |
2684 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2816 KB |
Output is correct |
4 |
Correct |
2 ms |
2900 KB |
Output is correct |
5 |
Correct |
2 ms |
2900 KB |
Output is correct |
6 |
Correct |
2 ms |
2816 KB |
Output is correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Correct |
2 ms |
2816 KB |
Output is correct |
9 |
Correct |
2 ms |
2820 KB |
Output is correct |
10 |
Correct |
2 ms |
2900 KB |
Output is correct |
11 |
Correct |
2 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
2 ms |
2820 KB |
Output is correct |
14 |
Correct |
2 ms |
2816 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2816 KB |
Output is correct |
18 |
Correct |
2 ms |
2900 KB |
Output is correct |
19 |
Correct |
2 ms |
2900 KB |
Output is correct |
20 |
Correct |
2 ms |
2900 KB |
Output is correct |
21 |
Correct |
2 ms |
2900 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2808 KB |
Output is correct |
26 |
Correct |
2 ms |
2816 KB |
Output is correct |
27 |
Correct |
2 ms |
2812 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
2 ms |
2900 KB |
Output is correct |
30 |
Correct |
2 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
1 ms |
2700 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
1 ms |
2680 KB |
Output is correct |
11 |
Correct |
2 ms |
2680 KB |
Output is correct |
12 |
Correct |
1 ms |
2684 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2816 KB |
Output is correct |
21 |
Correct |
2 ms |
2900 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Correct |
2 ms |
2816 KB |
Output is correct |
24 |
Correct |
2 ms |
2900 KB |
Output is correct |
25 |
Correct |
2 ms |
2816 KB |
Output is correct |
26 |
Correct |
2 ms |
2820 KB |
Output is correct |
27 |
Correct |
2 ms |
2900 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
2 ms |
2900 KB |
Output is correct |
30 |
Correct |
2 ms |
2820 KB |
Output is correct |
31 |
Correct |
2 ms |
2816 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2816 KB |
Output is correct |
35 |
Correct |
2 ms |
2900 KB |
Output is correct |
36 |
Correct |
2 ms |
2900 KB |
Output is correct |
37 |
Correct |
2 ms |
2900 KB |
Output is correct |
38 |
Correct |
2 ms |
2900 KB |
Output is correct |
39 |
Correct |
2 ms |
2900 KB |
Output is correct |
40 |
Correct |
2 ms |
2772 KB |
Output is correct |
41 |
Correct |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2808 KB |
Output is correct |
43 |
Correct |
2 ms |
2816 KB |
Output is correct |
44 |
Correct |
2 ms |
2812 KB |
Output is correct |
45 |
Correct |
2 ms |
2900 KB |
Output is correct |
46 |
Correct |
2 ms |
2900 KB |
Output is correct |
47 |
Correct |
2 ms |
2900 KB |
Output is correct |
48 |
Correct |
83 ms |
15024 KB |
Output is correct |
49 |
Correct |
83 ms |
16184 KB |
Output is correct |
50 |
Correct |
78 ms |
16248 KB |
Output is correct |
51 |
Correct |
55 ms |
13452 KB |
Output is correct |
52 |
Correct |
76 ms |
16072 KB |
Output is correct |
53 |
Correct |
60 ms |
14924 KB |
Output is correct |
54 |
Correct |
42 ms |
11724 KB |
Output is correct |
55 |
Correct |
82 ms |
20512 KB |
Output is correct |
56 |
Correct |
108 ms |
18208 KB |
Output is correct |
57 |
Correct |
112 ms |
17456 KB |
Output is correct |
58 |
Correct |
107 ms |
16836 KB |
Output is correct |
59 |
Correct |
86 ms |
16228 KB |
Output is correct |
60 |
Correct |
62 ms |
16392 KB |
Output is correct |
61 |
Correct |
68 ms |
16452 KB |
Output is correct |
62 |
Correct |
96 ms |
16632 KB |
Output is correct |
63 |
Correct |
62 ms |
15896 KB |
Output is correct |
64 |
Correct |
71 ms |
16528 KB |
Output is correct |
65 |
Correct |
100 ms |
16656 KB |
Output is correct |
66 |
Correct |
64 ms |
15876 KB |
Output is correct |
67 |
Correct |
43 ms |
13124 KB |
Output is correct |
68 |
Correct |
64 ms |
14672 KB |
Output is correct |
69 |
Correct |
66 ms |
16604 KB |
Output is correct |
70 |
Correct |
57 ms |
15860 KB |
Output is correct |
71 |
Correct |
40 ms |
12712 KB |
Output is correct |
72 |
Correct |
49 ms |
13976 KB |
Output is correct |
73 |
Correct |
64 ms |
16620 KB |
Output is correct |
74 |
Correct |
71 ms |
15640 KB |
Output is correct |
75 |
Correct |
68 ms |
16016 KB |
Output is correct |
76 |
Correct |
78 ms |
16088 KB |
Output is correct |
77 |
Correct |
82 ms |
15984 KB |
Output is correct |
78 |
Correct |
39 ms |
12064 KB |
Output is correct |
79 |
Correct |
65 ms |
13516 KB |
Output is correct |
80 |
Correct |
71 ms |
16068 KB |
Output is correct |
81 |
Correct |
73 ms |
16160 KB |
Output is correct |
82 |
Correct |
72 ms |
16448 KB |
Output is correct |