#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);
iota(amin.begin(), amin.end(), 0);
int ansmin = 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;
}
set<int> s;
for (int i = 0; i < n; i++) {
s.insert(i);
}
auto bfs = [&](int src) {
vector<int> dis(n, -1);
queue<int> q;
dis[src] = 0;
q.push(src);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int y : adj[x]) {
if (dis[y] == -1 && s.count(y)) {
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
return dis;
};
vector<int> amax(n);
iota(amax.begin(), amax.end(), 0);
int64_t ansmax = 0;
if (n <= 1000) {
while (s.size() > 1) {
int t = *s.begin();
auto dis = bfs(t);
int u = max_element(dis.begin(), dis.end()) - dis.begin();
dis = bfs(u);
int v = max_element(dis.begin(), dis.end()) - dis.begin();
swap(amax[u], amax[v]);
ansmax += 2 * dis[v];
s.erase(u);
s.erase(v);
}
if (s.size() == 1) {
int x = *s.begin();
swap(amax[x], amax[adj[x][0]]);
}
}
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:33:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int i = 1; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
Village.cpp:39:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 2; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
Village.cpp: In instantiation of 'main()::<lambda(auto:23, int, int)> [with auto:23 = main()::<lambda(auto:23, int, int)>]':
Village.cpp:45:23: required from here
Village.cpp:33:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int i = 1; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
Village.cpp:39:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 2; i < b.size(); i += 2) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 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 |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
7 ms |
320 KB |
Output is correct |
3 |
Correct |
7 ms |
340 KB |
Output is correct |
4 |
Partially correct |
33 ms |
340 KB |
Partially correct |
5 |
Partially correct |
36 ms |
340 KB |
Partially correct |
6 |
Correct |
35 ms |
440 KB |
Output is correct |
7 |
Correct |
34 ms |
528 KB |
Output is correct |
8 |
Correct |
33 ms |
340 KB |
Output is correct |
9 |
Correct |
27 ms |
460 KB |
Output is correct |
10 |
Correct |
33 ms |
472 KB |
Output is correct |
11 |
Correct |
36 ms |
468 KB |
Output is correct |
12 |
Correct |
31 ms |
340 KB |
Output is correct |
13 |
Correct |
33 ms |
320 KB |
Output is correct |
14 |
Correct |
38 ms |
448 KB |
Output is correct |
15 |
Correct |
30 ms |
340 KB |
Output is correct |
16 |
Correct |
34 ms |
340 KB |
Output is correct |
17 |
Correct |
34 ms |
340 KB |
Output is correct |
18 |
Correct |
33 ms |
432 KB |
Output is correct |
19 |
Correct |
37 ms |
340 KB |
Output is correct |
20 |
Correct |
35 ms |
436 KB |
Output is correct |
21 |
Correct |
36 ms |
340 KB |
Output is correct |
22 |
Correct |
37 ms |
468 KB |
Output is correct |
23 |
Correct |
27 ms |
340 KB |
Output is correct |
24 |
Partially correct |
26 ms |
340 KB |
Partially correct |
25 |
Correct |
9 ms |
340 KB |
Output is correct |
26 |
Correct |
41 ms |
340 KB |
Output is correct |
27 |
Correct |
10 ms |
340 KB |
Output is correct |
28 |
Correct |
44 ms |
448 KB |
Output is correct |
29 |
Correct |
40 ms |
448 KB |
Output is correct |
30 |
Correct |
41 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 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 |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
7 ms |
320 KB |
Output is correct |
20 |
Correct |
7 ms |
340 KB |
Output is correct |
21 |
Partially correct |
33 ms |
340 KB |
Partially correct |
22 |
Partially correct |
36 ms |
340 KB |
Partially correct |
23 |
Correct |
35 ms |
440 KB |
Output is correct |
24 |
Correct |
34 ms |
528 KB |
Output is correct |
25 |
Correct |
33 ms |
340 KB |
Output is correct |
26 |
Correct |
27 ms |
460 KB |
Output is correct |
27 |
Correct |
33 ms |
472 KB |
Output is correct |
28 |
Correct |
36 ms |
468 KB |
Output is correct |
29 |
Correct |
31 ms |
340 KB |
Output is correct |
30 |
Correct |
33 ms |
320 KB |
Output is correct |
31 |
Correct |
38 ms |
448 KB |
Output is correct |
32 |
Correct |
30 ms |
340 KB |
Output is correct |
33 |
Correct |
34 ms |
340 KB |
Output is correct |
34 |
Correct |
34 ms |
340 KB |
Output is correct |
35 |
Correct |
33 ms |
432 KB |
Output is correct |
36 |
Correct |
37 ms |
340 KB |
Output is correct |
37 |
Correct |
35 ms |
436 KB |
Output is correct |
38 |
Correct |
36 ms |
340 KB |
Output is correct |
39 |
Correct |
37 ms |
468 KB |
Output is correct |
40 |
Correct |
27 ms |
340 KB |
Output is correct |
41 |
Partially correct |
26 ms |
340 KB |
Partially correct |
42 |
Correct |
9 ms |
340 KB |
Output is correct |
43 |
Correct |
41 ms |
340 KB |
Output is correct |
44 |
Correct |
10 ms |
340 KB |
Output is correct |
45 |
Correct |
44 ms |
448 KB |
Output is correct |
46 |
Correct |
40 ms |
448 KB |
Output is correct |
47 |
Correct |
41 ms |
340 KB |
Output is correct |
48 |
Partially correct |
73 ms |
11700 KB |
Partially correct |
49 |
Partially correct |
108 ms |
12840 KB |
Partially correct |
50 |
Partially correct |
88 ms |
12868 KB |
Partially correct |
51 |
Partially correct |
73 ms |
10036 KB |
Partially correct |
52 |
Partially correct |
88 ms |
12596 KB |
Partially correct |
53 |
Partially correct |
72 ms |
11400 KB |
Partially correct |
54 |
Partially correct |
42 ms |
13612 KB |
Partially correct |
55 |
Partially correct |
99 ms |
27808 KB |
Partially correct |
56 |
Partially correct |
111 ms |
19872 KB |
Partially correct |
57 |
Partially correct |
93 ms |
17328 KB |
Partially correct |
58 |
Partially correct |
95 ms |
15156 KB |
Partially correct |
59 |
Partially correct |
81 ms |
13036 KB |
Partially correct |
60 |
Partially correct |
64 ms |
13072 KB |
Partially correct |
61 |
Partially correct |
73 ms |
13052 KB |
Partially correct |
62 |
Partially correct |
75 ms |
13172 KB |
Partially correct |
63 |
Partially correct |
85 ms |
12364 KB |
Partially correct |
64 |
Partially correct |
71 ms |
13128 KB |
Partially correct |
65 |
Partially correct |
76 ms |
13164 KB |
Partially correct |
66 |
Partially correct |
60 ms |
12360 KB |
Partially correct |
67 |
Partially correct |
50 ms |
9848 KB |
Partially correct |
68 |
Partially correct |
56 ms |
11212 KB |
Partially correct |
69 |
Partially correct |
65 ms |
13248 KB |
Partially correct |
70 |
Partially correct |
62 ms |
12420 KB |
Partially correct |
71 |
Partially correct |
46 ms |
9396 KB |
Partially correct |
72 |
Partially correct |
60 ms |
10504 KB |
Partially correct |
73 |
Partially correct |
73 ms |
13248 KB |
Partially correct |
74 |
Partially correct |
82 ms |
12156 KB |
Partially correct |
75 |
Partially correct |
78 ms |
12700 KB |
Partially correct |
76 |
Partially correct |
87 ms |
12684 KB |
Partially correct |
77 |
Partially correct |
76 ms |
12568 KB |
Partially correct |
78 |
Partially correct |
56 ms |
8768 KB |
Partially correct |
79 |
Partially correct |
59 ms |
10096 KB |
Partially correct |
80 |
Partially correct |
83 ms |
12704 KB |
Partially correct |
81 |
Partially correct |
75 ms |
12620 KB |
Partially correct |
82 |
Partially correct |
83 ms |
13016 KB |
Partially correct |