# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
634738 |
2022-08-24T19:02:57 Z |
Olympia |
Village (BOI20_village) |
C++17 |
|
221 ms |
34836 KB |
#include <bits/stdc++.h>
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) {
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: In function 'int main()':
Village.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < ord.size(); i++) {
| ~~^~~~~~~~~~~~
# |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 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 |
2 ms |
580 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
584 KB |
Output is correct |
8 |
Correct |
1 ms |
584 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
584 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
576 KB |
Output is correct |
15 |
Correct |
1 ms |
532 KB |
Output is correct |
16 |
Correct |
1 ms |
580 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
572 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
468 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 |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
2 ms |
596 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
316 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 |
2 ms |
580 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
584 KB |
Output is correct |
25 |
Correct |
1 ms |
584 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
584 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
576 KB |
Output is correct |
32 |
Correct |
1 ms |
532 KB |
Output is correct |
33 |
Correct |
1 ms |
580 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
572 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
2 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
2 ms |
596 KB |
Output is correct |
48 |
Correct |
141 ms |
26328 KB |
Output is correct |
49 |
Correct |
157 ms |
28992 KB |
Output is correct |
50 |
Correct |
157 ms |
28968 KB |
Output is correct |
51 |
Correct |
117 ms |
22604 KB |
Output is correct |
52 |
Correct |
147 ms |
28648 KB |
Output is correct |
53 |
Correct |
136 ms |
25844 KB |
Output is correct |
54 |
Correct |
83 ms |
17392 KB |
Output is correct |
55 |
Correct |
221 ms |
34836 KB |
Output is correct |
56 |
Correct |
207 ms |
31820 KB |
Output is correct |
57 |
Correct |
200 ms |
30432 KB |
Output is correct |
58 |
Correct |
189 ms |
29900 KB |
Output is correct |
59 |
Correct |
163 ms |
29072 KB |
Output is correct |
60 |
Correct |
130 ms |
29124 KB |
Output is correct |
61 |
Correct |
149 ms |
29328 KB |
Output is correct |
62 |
Correct |
154 ms |
29364 KB |
Output is correct |
63 |
Correct |
143 ms |
27540 KB |
Output is correct |
64 |
Correct |
150 ms |
29248 KB |
Output is correct |
65 |
Correct |
144 ms |
29420 KB |
Output is correct |
66 |
Correct |
134 ms |
27596 KB |
Output is correct |
67 |
Correct |
99 ms |
21644 KB |
Output is correct |
68 |
Correct |
120 ms |
24908 KB |
Output is correct |
69 |
Correct |
139 ms |
29444 KB |
Output is correct |
70 |
Correct |
140 ms |
27620 KB |
Output is correct |
71 |
Correct |
93 ms |
20624 KB |
Output is correct |
72 |
Correct |
104 ms |
23292 KB |
Output is correct |
73 |
Correct |
142 ms |
29420 KB |
Output is correct |
74 |
Correct |
123 ms |
26852 KB |
Output is correct |
75 |
Correct |
157 ms |
28816 KB |
Output is correct |
76 |
Correct |
154 ms |
28792 KB |
Output is correct |
77 |
Correct |
133 ms |
27708 KB |
Output is correct |
78 |
Correct |
92 ms |
19528 KB |
Output is correct |
79 |
Correct |
116 ms |
22572 KB |
Output is correct |
80 |
Correct |
153 ms |
28868 KB |
Output is correct |
81 |
Correct |
151 ms |
27764 KB |
Output is correct |
82 |
Correct |
138 ms |
29044 KB |
Output is correct |