#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;
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) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 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 |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
9 ms |
380 KB |
Output is correct |
3 |
Correct |
8 ms |
380 KB |
Output is correct |
4 |
Partially correct |
36 ms |
468 KB |
Partially correct |
5 |
Partially correct |
33 ms |
340 KB |
Partially correct |
6 |
Correct |
33 ms |
340 KB |
Output is correct |
7 |
Correct |
44 ms |
468 KB |
Output is correct |
8 |
Correct |
37 ms |
428 KB |
Output is correct |
9 |
Correct |
26 ms |
436 KB |
Output is correct |
10 |
Correct |
33 ms |
472 KB |
Output is correct |
11 |
Correct |
32 ms |
468 KB |
Output is correct |
12 |
Correct |
29 ms |
340 KB |
Output is correct |
13 |
Correct |
31 ms |
340 KB |
Output is correct |
14 |
Correct |
32 ms |
444 KB |
Output is correct |
15 |
Correct |
28 ms |
432 KB |
Output is correct |
16 |
Correct |
39 ms |
340 KB |
Output is correct |
17 |
Correct |
33 ms |
340 KB |
Output is correct |
18 |
Correct |
31 ms |
440 KB |
Output is correct |
19 |
Correct |
32 ms |
340 KB |
Output is correct |
20 |
Correct |
30 ms |
420 KB |
Output is correct |
21 |
Correct |
33 ms |
340 KB |
Output is correct |
22 |
Correct |
38 ms |
436 KB |
Output is correct |
23 |
Correct |
27 ms |
428 KB |
Output is correct |
24 |
Partially correct |
27 ms |
340 KB |
Partially correct |
25 |
Correct |
11 ms |
376 KB |
Output is correct |
26 |
Correct |
42 ms |
340 KB |
Output is correct |
27 |
Correct |
9 ms |
340 KB |
Output is correct |
28 |
Correct |
40 ms |
428 KB |
Output is correct |
29 |
Correct |
40 ms |
340 KB |
Output is correct |
30 |
Correct |
38 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
9 ms |
380 KB |
Output is correct |
20 |
Correct |
8 ms |
380 KB |
Output is correct |
21 |
Partially correct |
36 ms |
468 KB |
Partially correct |
22 |
Partially correct |
33 ms |
340 KB |
Partially correct |
23 |
Correct |
33 ms |
340 KB |
Output is correct |
24 |
Correct |
44 ms |
468 KB |
Output is correct |
25 |
Correct |
37 ms |
428 KB |
Output is correct |
26 |
Correct |
26 ms |
436 KB |
Output is correct |
27 |
Correct |
33 ms |
472 KB |
Output is correct |
28 |
Correct |
32 ms |
468 KB |
Output is correct |
29 |
Correct |
29 ms |
340 KB |
Output is correct |
30 |
Correct |
31 ms |
340 KB |
Output is correct |
31 |
Correct |
32 ms |
444 KB |
Output is correct |
32 |
Correct |
28 ms |
432 KB |
Output is correct |
33 |
Correct |
39 ms |
340 KB |
Output is correct |
34 |
Correct |
33 ms |
340 KB |
Output is correct |
35 |
Correct |
31 ms |
440 KB |
Output is correct |
36 |
Correct |
32 ms |
340 KB |
Output is correct |
37 |
Correct |
30 ms |
420 KB |
Output is correct |
38 |
Correct |
33 ms |
340 KB |
Output is correct |
39 |
Correct |
38 ms |
436 KB |
Output is correct |
40 |
Correct |
27 ms |
428 KB |
Output is correct |
41 |
Partially correct |
27 ms |
340 KB |
Partially correct |
42 |
Correct |
11 ms |
376 KB |
Output is correct |
43 |
Correct |
42 ms |
340 KB |
Output is correct |
44 |
Correct |
9 ms |
340 KB |
Output is correct |
45 |
Correct |
40 ms |
428 KB |
Output is correct |
46 |
Correct |
40 ms |
340 KB |
Output is correct |
47 |
Correct |
38 ms |
340 KB |
Output is correct |
48 |
Execution timed out |
1071 ms |
11192 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |