#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int x; cin >> x; x--;
int y; cin >> y; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int> amin(n), amax(n);
iota(amin.begin(), amin.end(), 0);
int ansmin = 0;
int64_t ansmax = 0;
auto dfs = [&](auto dfs, int x, int p) -> bool {
vector<int> b;
for (int y : adj[x]) {
if (y != p) {
if (dfs(dfs, y, x)) {
b.push_back(y);
}
}
}
if (b.empty()) {
return 1;
}
ansmin += b.size() * 2;
if (b.size() % 2 == 1) {
swap(amin[x], amin[b[0]]);
for (int i = 1; i < b.size(); i += 2) {
swap(amin[b[i]], amin[b[i + 1]]);
}
} else {
swap(amin[x], amin[b[0]]);
swap(amin[x], amin[b[1]]);
for (int i = 2; i < b.size(); i += 2) {
swap(amin[b[i]], amin[b[i + 1]]);
}
}
return 0;
};
if (dfs(dfs, 0, -1)) {
swap(amin[0], amin[adj[0][0]]);
ansmin += 2;
}
vector<int> sz(n);
auto init = [&](auto init, int x, int p) -> void {
sz[x] = 1;
for (int y : adj[x]) {
if (y != p) {
init(init, y, x);
sz[x] += sz[y];
}
}
ansmax += 2 * min(sz[x], n - sz[x]);
};
auto find = [&](auto find, int x, int p) -> int {
for (int y : adj[x]) {
if (y != p && sz[y] * 2 > n) {
return find(find, y, x);
}
}
return x;
};
init(init, 0, -1);
int c = find(find, 0, -1);
vector<int> b;
auto dfs2 = [&](auto dfs2, int x, int p) -> void {
b.push_back(x);
for (int y : adj[x]) {
if (y != p) {
dfs2(dfs2, y, x);
}
}
};
dfs2(dfs2, 0, -1);
for (int i = 0; i < n; i++) {
amax[b[i]] = b[(i + n / 2) % n];
}
cout << ansmin << " " << ansmax << "\n";
for (int i = 0; i < n; i++) {
cout << amin[i] + 1 << " \n"[i == n - 1];
}
for (int i = 0; i < n; i++) {
cout << amax[i] + 1 << " \n"[i == n - 1];
}
return 6/22;
}
Compilation message
Village.cpp: In lambda function:
Village.cpp:34:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i = 1; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
Village.cpp:40:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 2; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:70:9: warning: unused variable 'c' [-Wunused-variable]
70 | int c = find(find, 0, -1);
| ^
Village.cpp: In instantiation of 'main()::<lambda(auto:23, int, int)> [with auto:23 = main()::<lambda(auto:23, int, int)>]':
Village.cpp:46:23: required from here
Village.cpp:34:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int i = 1; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
Village.cpp:40:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 2; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
272 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
260 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
272 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
260 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
352 KB |
Output is correct |
32 |
Correct |
1 ms |
352 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
68 ms |
8068 KB |
Output is correct |
49 |
Correct |
83 ms |
8780 KB |
Output is correct |
50 |
Correct |
89 ms |
8724 KB |
Output is correct |
51 |
Correct |
50 ms |
6852 KB |
Output is correct |
52 |
Correct |
98 ms |
8668 KB |
Output is correct |
53 |
Correct |
58 ms |
7884 KB |
Output is correct |
54 |
Correct |
38 ms |
11688 KB |
Output is correct |
55 |
Correct |
94 ms |
23796 KB |
Output is correct |
56 |
Correct |
100 ms |
15824 KB |
Output is correct |
57 |
Correct |
77 ms |
13472 KB |
Output is correct |
58 |
Correct |
88 ms |
11232 KB |
Output is correct |
59 |
Correct |
79 ms |
8872 KB |
Output is correct |
60 |
Correct |
47 ms |
9620 KB |
Output is correct |
61 |
Correct |
53 ms |
9032 KB |
Output is correct |
62 |
Correct |
60 ms |
9280 KB |
Output is correct |
63 |
Correct |
53 ms |
8660 KB |
Output is correct |
64 |
Correct |
70 ms |
9080 KB |
Output is correct |
65 |
Correct |
55 ms |
9192 KB |
Output is correct |
66 |
Correct |
44 ms |
8584 KB |
Output is correct |
67 |
Correct |
33 ms |
6776 KB |
Output is correct |
68 |
Correct |
59 ms |
7828 KB |
Output is correct |
69 |
Correct |
62 ms |
9240 KB |
Output is correct |
70 |
Correct |
52 ms |
8672 KB |
Output is correct |
71 |
Correct |
34 ms |
6568 KB |
Output is correct |
72 |
Correct |
44 ms |
7376 KB |
Output is correct |
73 |
Correct |
56 ms |
9252 KB |
Output is correct |
74 |
Correct |
47 ms |
8452 KB |
Output is correct |
75 |
Correct |
61 ms |
8636 KB |
Output is correct |
76 |
Correct |
75 ms |
8552 KB |
Output is correct |
77 |
Correct |
58 ms |
8704 KB |
Output is correct |
78 |
Correct |
36 ms |
6080 KB |
Output is correct |
79 |
Correct |
49 ms |
6928 KB |
Output is correct |
80 |
Correct |
61 ms |
8576 KB |
Output is correct |
81 |
Correct |
77 ms |
8864 KB |
Output is correct |
82 |
Correct |
55 ms |
9132 KB |
Output is correct |