#include <cmath>
#include <iostream>
#include <set>
#include <climits>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <type_traits>
#include <string>
#include <queue>
#include <map>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
vector<vector<int>> adj;
vector<int> ord;
int t = 0;
int c = 0;
vector<int64_t> pre, post, depth, sub;
vector<vector<int>> dp;
bool isAncestor(int u, int v) {
return (pre[u] <= pre[v] && post[u] >= post[v]);
}
int lca(int u, int v) {
if (isAncestor(u, v)) return u;
if (isAncestor(v, u)) return v;
for (int i = 19; i >= 0; i--) if (!isAncestor(dp[u][i], v))u = dp[u][i];
return dp[u][0];
}
void find_sub(int curNode, int prevNode) {
sub[curNode] = 1;
for (int i: adj[curNode]) {
if (i != prevNode) {
find_sub(i, curNode);
sub[curNode] += sub[i];
}
}
}
int get_centroid(int curNode, int prevNode) {
for (int i: adj[curNode]) {
if (i == prevNode) continue;
if (sub[i] > (int) adj.size() / 2) {
return get_centroid(i, curNode);
}
}
return curNode;
}
void f(int curNode, int prevNode) {
depth[curNode] = depth[prevNode] + 1;
dp[curNode].resize(20);
dp[curNode][0] = prevNode;
for (int i = 1; i < 20; i++)dp[curNode][i] = dp[dp[curNode][i - 1]][i - 1];
ord[curNode] = t++, pre[curNode] = c++;
for (int i:adj[curNode]) if (i != prevNode)f(i, curNode);
post[curNode] = c++;
}
vector<int> ans;
int tot = 0;
void dfs (int curNode, int prevNode) {
//cout << curNode << " " << prevNode << endl;
for (int i: adj[curNode]) {
if (i == prevNode) {
continue;
}
dfs(i, curNode);
if (ans[curNode] == -1 && ans[i] == -1) {
ans[curNode] = i;
ans[i] = curNode;
tot++;
} else if (ans[i] == -1){
ans[i] = ans[curNode];
ans[curNode] = i;
tot++;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
adj.resize(n), depth.assign(n, false), dp.resize(n), pre.resize(n), post.resize(n), sub.resize(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v), adj[v].push_back(u);
}
find_sub(0, -1);
ord.resize(n), ans.assign(n, -1);
int centroid = get_centroid(0, -1);
f(centroid, centroid);
dfs(0, -1);
if (ans[0] == -1) {
ans[0] = ans[adj[0].back()];
ans[adj[0].back()] = 0;
tot++;
}
int64_t al = 0;
map<int, int> myMap;
for (int i = 0; i < ord.size(); i++) myMap[ord[i]] = i;
vector<int> ansr(n);
for (int i = 0; i < n; i++) ansr[myMap[i]] = myMap[(i + n / 2 + n) % n];
for (int i = 0; i < n; i++) al += depth[i] + depth[ansr[i]] - 2 * depth[lca(i, ansr[i])];
cout << tot * 2 << " " << al << '\n';
for (int i = 0; i < n; i++) cout << ans[i] + 1 << ' ';
cout << '\n';
for (int i: ansr) cout << i + 1 << ' ';
}
Compilation message
Village.cpp:18: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
18 | #pragma GCC optimization ("O3")
|
Village.cpp:19: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
19 | #pragma GCC optimization ("unroll-loops")
|
Village.cpp: In function 'int main()':
Village.cpp:120:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int i = 0; i < ord.size(); i++) myMap[ord[i]] = i;
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 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 |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
584 KB |
Output is correct |
8 |
Correct |
2 ms |
576 KB |
Output is correct |
9 |
Correct |
2 ms |
580 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
580 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
576 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
448 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
2 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 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 |
0 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 |
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 |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
584 KB |
Output is correct |
25 |
Correct |
2 ms |
576 KB |
Output is correct |
26 |
Correct |
2 ms |
580 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
2 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
580 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
596 KB |
Output is correct |
37 |
Correct |
2 ms |
576 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
448 KB |
Output is correct |
45 |
Correct |
2 ms |
596 KB |
Output is correct |
46 |
Correct |
2 ms |
596 KB |
Output is correct |
47 |
Correct |
2 ms |
596 KB |
Output is correct |
48 |
Correct |
153 ms |
26356 KB |
Output is correct |
49 |
Correct |
178 ms |
28956 KB |
Output is correct |
50 |
Correct |
161 ms |
28976 KB |
Output is correct |
51 |
Correct |
144 ms |
22628 KB |
Output is correct |
52 |
Correct |
153 ms |
28600 KB |
Output is correct |
53 |
Correct |
136 ms |
25804 KB |
Output is correct |
54 |
Correct |
88 ms |
17268 KB |
Output is correct |
55 |
Correct |
222 ms |
34828 KB |
Output is correct |
56 |
Correct |
208 ms |
31792 KB |
Output is correct |
57 |
Correct |
210 ms |
30460 KB |
Output is correct |
58 |
Correct |
189 ms |
29820 KB |
Output is correct |
59 |
Correct |
167 ms |
29004 KB |
Output is correct |
60 |
Correct |
129 ms |
29072 KB |
Output is correct |
61 |
Correct |
147 ms |
29260 KB |
Output is correct |
62 |
Correct |
160 ms |
29296 KB |
Output is correct |
63 |
Correct |
148 ms |
27588 KB |
Output is correct |
64 |
Correct |
145 ms |
29376 KB |
Output is correct |
65 |
Correct |
167 ms |
29460 KB |
Output is correct |
66 |
Correct |
132 ms |
27520 KB |
Output is correct |
67 |
Correct |
104 ms |
21732 KB |
Output is correct |
68 |
Correct |
119 ms |
24908 KB |
Output is correct |
69 |
Correct |
143 ms |
29544 KB |
Output is correct |
70 |
Correct |
139 ms |
27544 KB |
Output is correct |
71 |
Correct |
96 ms |
20632 KB |
Output is correct |
72 |
Correct |
108 ms |
23300 KB |
Output is correct |
73 |
Correct |
140 ms |
29460 KB |
Output is correct |
74 |
Correct |
131 ms |
26932 KB |
Output is correct |
75 |
Correct |
171 ms |
28824 KB |
Output is correct |
76 |
Correct |
153 ms |
28752 KB |
Output is correct |
77 |
Correct |
139 ms |
27760 KB |
Output is correct |
78 |
Correct |
93 ms |
19532 KB |
Output is correct |
79 |
Correct |
107 ms |
22572 KB |
Output is correct |
80 |
Correct |
151 ms |
28940 KB |
Output is correct |
81 |
Correct |
147 ms |
27844 KB |
Output is correct |
82 |
Correct |
143 ms |
29000 KB |
Output is correct |