#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5, MAX_L = __lg(MAX_N) + 1;
vector<int> G[MAX_N];
int up[MAX_L][MAX_N];
int depth[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 = 1; i < MAX_L; i++) {
up[i][u] = up[i - 1][up[i - 1][u]];
}
for (int v : G[u]) {
if (v != p) {
DFS(v, u);
}
}
}
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);
}
DFS(0, 0);
int64_t smallest = 0, largest = 0;
queue<int> leaves;
vector<int> value(N), degree(N);
iota(value.begin(), value.end(), 0);
for (int i = 0; i < N; i++) {
degree[i] = (int)G[i].size();
if (degree[i] == 1) {
leaves.push(i);
}
}
vector<bool> deleted(N, false);
while (!leaves.empty()) {
int u = leaves.front();
leaves.pop();
deleted[u] = true;
for (int v : G[u]) {
if (!deleted[v]) {
degree[v]--;
if (degree[v] == 1) {
if (value[u] == u) {
smallest += 2;
swap(value[u], value[v]);
}
leaves.push(v);
}
}
}
if (value[u] == u) {
smallest += 2;
swap(value[u], value[G[u][0]]);
}
}
vector<int> large(N);
for (int i = 0; i < N; i++) {
largest += distance(ord[i], ord[(i + N / 2) % N]);
large[ord[i]] = ord[(i + N / 2) % N];
}
cout << smallest << " " << largest << "\n";
for (int i = 0; i < N; i++) {
cout << 1 + value[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 |
2680 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2676 KB |
Output is correct |
7 |
Correct |
1 ms |
2676 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2680 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2804 KB |
Output is correct |
3 |
Partially correct |
1 ms |
2800 KB |
Partially correct |
4 |
Correct |
2 ms |
2812 KB |
Output is correct |
5 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
6 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
7 |
Correct |
2 ms |
2900 KB |
Output is correct |
8 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
9 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
10 |
Correct |
2 ms |
2900 KB |
Output is correct |
11 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2812 KB |
Output is correct |
14 |
Correct |
2 ms |
2900 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2812 KB |
Output is correct |
17 |
Correct |
2 ms |
2900 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 |
2808 KB |
Output is correct |
22 |
Correct |
2 ms |
2900 KB |
Output is correct |
23 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
24 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Partially correct |
2 ms |
2900 KB |
Partially 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 |
2680 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2676 KB |
Output is correct |
7 |
Correct |
1 ms |
2676 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2680 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2672 KB |
Output is correct |
18 |
Correct |
1 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2804 KB |
Output is correct |
20 |
Partially correct |
1 ms |
2800 KB |
Partially correct |
21 |
Correct |
2 ms |
2812 KB |
Output is correct |
22 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
23 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
24 |
Correct |
2 ms |
2900 KB |
Output is correct |
25 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
26 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
27 |
Correct |
2 ms |
2900 KB |
Output is correct |
28 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2812 KB |
Output is correct |
31 |
Correct |
2 ms |
2900 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2812 KB |
Output is correct |
34 |
Correct |
2 ms |
2900 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 |
2808 KB |
Output is correct |
39 |
Correct |
2 ms |
2900 KB |
Output is correct |
40 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
41 |
Partially correct |
2 ms |
2772 KB |
Partially correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Partially correct |
2 ms |
2900 KB |
Partially correct |
46 |
Correct |
2 ms |
2900 KB |
Output is correct |
47 |
Correct |
2 ms |
2900 KB |
Output is correct |
48 |
Partially correct |
76 ms |
15912 KB |
Partially correct |
49 |
Partially correct |
68 ms |
17064 KB |
Partially correct |
50 |
Partially correct |
67 ms |
17092 KB |
Partially correct |
51 |
Partially correct |
58 ms |
13924 KB |
Partially correct |
52 |
Partially correct |
77 ms |
16964 KB |
Partially correct |
53 |
Partially correct |
59 ms |
15580 KB |
Partially correct |
54 |
Correct |
30 ms |
11900 KB |
Output is correct |
55 |
Correct |
86 ms |
21360 KB |
Output is correct |
56 |
Partially correct |
82 ms |
19024 KB |
Partially correct |
57 |
Partially correct |
76 ms |
18168 KB |
Partially correct |
58 |
Partially correct |
78 ms |
17660 KB |
Partially correct |
59 |
Partially correct |
88 ms |
17164 KB |
Partially correct |
60 |
Correct |
66 ms |
17376 KB |
Output is correct |
61 |
Correct |
60 ms |
17620 KB |
Output is correct |
62 |
Correct |
85 ms |
17568 KB |
Output is correct |
63 |
Correct |
71 ms |
16760 KB |
Output is correct |
64 |
Correct |
74 ms |
17552 KB |
Output is correct |
65 |
Correct |
73 ms |
17700 KB |
Output is correct |
66 |
Correct |
73 ms |
16764 KB |
Output is correct |
67 |
Correct |
41 ms |
13796 KB |
Output is correct |
68 |
Correct |
51 ms |
15484 KB |
Output is correct |
69 |
Correct |
70 ms |
17736 KB |
Output is correct |
70 |
Correct |
70 ms |
16816 KB |
Output is correct |
71 |
Correct |
57 ms |
13268 KB |
Output is correct |
72 |
Correct |
45 ms |
14644 KB |
Output is correct |
73 |
Correct |
98 ms |
17752 KB |
Output is correct |
74 |
Correct |
85 ms |
16456 KB |
Output is correct |
75 |
Partially correct |
86 ms |
16940 KB |
Partially correct |
76 |
Partially correct |
99 ms |
16988 KB |
Partially correct |
77 |
Correct |
79 ms |
16840 KB |
Output is correct |
78 |
Correct |
59 ms |
12504 KB |
Output is correct |
79 |
Partially correct |
46 ms |
14144 KB |
Partially correct |
80 |
Partially correct |
100 ms |
16936 KB |
Partially correct |
81 |
Correct |
67 ms |
17004 KB |
Output is correct |
82 |
Correct |
75 ms |
17452 KB |
Output is correct |