#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
const int MAX_N = 2e5 + 5;
const int MAX_K = 20;
vector<int> g[MAX_N];
int tin[MAX_N];
int tout[MAX_N];
int timer = 0;
int binup[MAX_K][MAX_N];
int depth[MAX_N];
int parent[MAX_N];
int find(int v) {
if (parent[v] == v) {
return v;
} else {
return parent[v] = find(parent[v]);
}
}
void dfs(int v, int p) {
parent[v] = v;
tin[v] = timer++;
for (auto u : g[v]) {
if (u == p) continue;
binup[0][u] = v;
depth[u] = depth[v] + 1;
dfs(u, v);
}
tout[v] = timer++;
}
bool is_parent(int p, int v) {
return (tin[p] <= tin[v] && tout[v] <= tout[p]);
}
int lca(int u, int v) {
if (is_parent(u, v)) return u;
if (is_parent(v, u)) return v;
for (int k = MAX_K - 1; k >= 0; --k) {
if (!is_parent(binup[k][v], u)) {
v = binup[k][v];
}
}
return binup[0][v];
}
int dist(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> p(n);
for (auto& x : p) {
cin >> x;
--x;
}
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
u = p[u];
v = p[v];
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0, -1);
for (int k = 1; k < MAX_K; ++k) {
for (int v = 0; v < n; ++v) {
binup[k][v] = binup[k - 1][binup[k - 1][v]];
}
}
vector<int64_t> dp(n);
for (int v = 0; v < n; ++v) {
for (auto u : g[v]) {
u = find(u);
if (u < v) {
parent[u] = v;
dp[v] = max(dp[v], dp[u] + dist(u, v));
}
}
}
cout << dp.back() << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21596 KB |
Output is correct |
2 |
Correct |
4 ms |
21688 KB |
Output is correct |
3 |
Correct |
3 ms |
21592 KB |
Output is correct |
4 |
Correct |
4 ms |
21596 KB |
Output is correct |
5 |
Correct |
3 ms |
21596 KB |
Output is correct |
6 |
Correct |
4 ms |
21596 KB |
Output is correct |
7 |
Correct |
3 ms |
21596 KB |
Output is correct |
8 |
Correct |
3 ms |
21596 KB |
Output is correct |
9 |
Correct |
4 ms |
21596 KB |
Output is correct |
10 |
Correct |
3 ms |
21676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21596 KB |
Output is correct |
2 |
Correct |
4 ms |
21688 KB |
Output is correct |
3 |
Correct |
3 ms |
21592 KB |
Output is correct |
4 |
Correct |
4 ms |
21596 KB |
Output is correct |
5 |
Correct |
3 ms |
21596 KB |
Output is correct |
6 |
Correct |
4 ms |
21596 KB |
Output is correct |
7 |
Correct |
3 ms |
21596 KB |
Output is correct |
8 |
Correct |
3 ms |
21596 KB |
Output is correct |
9 |
Correct |
4 ms |
21596 KB |
Output is correct |
10 |
Correct |
3 ms |
21676 KB |
Output is correct |
11 |
Correct |
4 ms |
21596 KB |
Output is correct |
12 |
Correct |
4 ms |
21596 KB |
Output is correct |
13 |
Correct |
3 ms |
21592 KB |
Output is correct |
14 |
Correct |
4 ms |
21592 KB |
Output is correct |
15 |
Correct |
4 ms |
21680 KB |
Output is correct |
16 |
Correct |
3 ms |
21596 KB |
Output is correct |
17 |
Correct |
4 ms |
21596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21596 KB |
Output is correct |
2 |
Correct |
4 ms |
21688 KB |
Output is correct |
3 |
Correct |
3 ms |
21592 KB |
Output is correct |
4 |
Correct |
4 ms |
21596 KB |
Output is correct |
5 |
Correct |
3 ms |
21596 KB |
Output is correct |
6 |
Correct |
4 ms |
21596 KB |
Output is correct |
7 |
Correct |
3 ms |
21596 KB |
Output is correct |
8 |
Correct |
3 ms |
21596 KB |
Output is correct |
9 |
Correct |
4 ms |
21596 KB |
Output is correct |
10 |
Correct |
3 ms |
21676 KB |
Output is correct |
11 |
Correct |
4 ms |
21596 KB |
Output is correct |
12 |
Correct |
4 ms |
21596 KB |
Output is correct |
13 |
Correct |
3 ms |
21592 KB |
Output is correct |
14 |
Correct |
4 ms |
21592 KB |
Output is correct |
15 |
Correct |
4 ms |
21680 KB |
Output is correct |
16 |
Correct |
3 ms |
21596 KB |
Output is correct |
17 |
Correct |
4 ms |
21596 KB |
Output is correct |
18 |
Correct |
6 ms |
22264 KB |
Output is correct |
19 |
Correct |
6 ms |
22228 KB |
Output is correct |
20 |
Correct |
5 ms |
22364 KB |
Output is correct |
21 |
Correct |
5 ms |
22108 KB |
Output is correct |
22 |
Correct |
5 ms |
22208 KB |
Output is correct |
23 |
Correct |
7 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21596 KB |
Output is correct |
2 |
Correct |
4 ms |
21688 KB |
Output is correct |
3 |
Correct |
3 ms |
21592 KB |
Output is correct |
4 |
Correct |
4 ms |
21596 KB |
Output is correct |
5 |
Correct |
3 ms |
21596 KB |
Output is correct |
6 |
Correct |
4 ms |
21596 KB |
Output is correct |
7 |
Correct |
3 ms |
21596 KB |
Output is correct |
8 |
Correct |
3 ms |
21596 KB |
Output is correct |
9 |
Correct |
4 ms |
21596 KB |
Output is correct |
10 |
Correct |
3 ms |
21676 KB |
Output is correct |
11 |
Correct |
4 ms |
21596 KB |
Output is correct |
12 |
Correct |
4 ms |
21596 KB |
Output is correct |
13 |
Correct |
3 ms |
21592 KB |
Output is correct |
14 |
Correct |
4 ms |
21592 KB |
Output is correct |
15 |
Correct |
4 ms |
21680 KB |
Output is correct |
16 |
Correct |
3 ms |
21596 KB |
Output is correct |
17 |
Correct |
4 ms |
21596 KB |
Output is correct |
18 |
Correct |
6 ms |
22264 KB |
Output is correct |
19 |
Correct |
6 ms |
22228 KB |
Output is correct |
20 |
Correct |
5 ms |
22364 KB |
Output is correct |
21 |
Correct |
5 ms |
22108 KB |
Output is correct |
22 |
Correct |
5 ms |
22208 KB |
Output is correct |
23 |
Correct |
7 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22108 KB |
Output is correct |
25 |
Correct |
3 ms |
21596 KB |
Output is correct |
26 |
Correct |
6 ms |
22108 KB |
Output is correct |
27 |
Correct |
5 ms |
22108 KB |
Output is correct |
28 |
Correct |
5 ms |
22108 KB |
Output is correct |
29 |
Correct |
5 ms |
22108 KB |
Output is correct |
30 |
Correct |
6 ms |
21852 KB |
Output is correct |
31 |
Correct |
5 ms |
21852 KB |
Output is correct |
32 |
Correct |
5 ms |
22032 KB |
Output is correct |
33 |
Correct |
6 ms |
21852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21596 KB |
Output is correct |
2 |
Correct |
4 ms |
21688 KB |
Output is correct |
3 |
Correct |
3 ms |
21592 KB |
Output is correct |
4 |
Correct |
4 ms |
21596 KB |
Output is correct |
5 |
Correct |
3 ms |
21596 KB |
Output is correct |
6 |
Correct |
4 ms |
21596 KB |
Output is correct |
7 |
Correct |
3 ms |
21596 KB |
Output is correct |
8 |
Correct |
3 ms |
21596 KB |
Output is correct |
9 |
Correct |
4 ms |
21596 KB |
Output is correct |
10 |
Correct |
3 ms |
21676 KB |
Output is correct |
11 |
Correct |
4 ms |
21596 KB |
Output is correct |
12 |
Correct |
4 ms |
21596 KB |
Output is correct |
13 |
Correct |
3 ms |
21592 KB |
Output is correct |
14 |
Correct |
4 ms |
21592 KB |
Output is correct |
15 |
Correct |
4 ms |
21680 KB |
Output is correct |
16 |
Correct |
3 ms |
21596 KB |
Output is correct |
17 |
Correct |
4 ms |
21596 KB |
Output is correct |
18 |
Correct |
6 ms |
22264 KB |
Output is correct |
19 |
Correct |
6 ms |
22228 KB |
Output is correct |
20 |
Correct |
5 ms |
22364 KB |
Output is correct |
21 |
Correct |
5 ms |
22108 KB |
Output is correct |
22 |
Correct |
5 ms |
22208 KB |
Output is correct |
23 |
Correct |
7 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22108 KB |
Output is correct |
25 |
Correct |
88 ms |
41044 KB |
Output is correct |
26 |
Correct |
77 ms |
47396 KB |
Output is correct |
27 |
Correct |
81 ms |
47140 KB |
Output is correct |
28 |
Correct |
118 ms |
46488 KB |
Output is correct |
29 |
Correct |
104 ms |
41044 KB |
Output is correct |
30 |
Correct |
102 ms |
44996 KB |
Output is correct |
31 |
Correct |
101 ms |
44164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21592 KB |
Output is correct |
2 |
Correct |
4 ms |
21596 KB |
Output is correct |
3 |
Correct |
124 ms |
34836 KB |
Output is correct |
4 |
Correct |
111 ms |
34684 KB |
Output is correct |
5 |
Correct |
119 ms |
34856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21596 KB |
Output is correct |
2 |
Correct |
4 ms |
21688 KB |
Output is correct |
3 |
Correct |
3 ms |
21592 KB |
Output is correct |
4 |
Correct |
4 ms |
21596 KB |
Output is correct |
5 |
Correct |
3 ms |
21596 KB |
Output is correct |
6 |
Correct |
4 ms |
21596 KB |
Output is correct |
7 |
Correct |
3 ms |
21596 KB |
Output is correct |
8 |
Correct |
3 ms |
21596 KB |
Output is correct |
9 |
Correct |
4 ms |
21596 KB |
Output is correct |
10 |
Correct |
3 ms |
21676 KB |
Output is correct |
11 |
Correct |
4 ms |
21596 KB |
Output is correct |
12 |
Correct |
4 ms |
21596 KB |
Output is correct |
13 |
Correct |
3 ms |
21592 KB |
Output is correct |
14 |
Correct |
4 ms |
21592 KB |
Output is correct |
15 |
Correct |
4 ms |
21680 KB |
Output is correct |
16 |
Correct |
3 ms |
21596 KB |
Output is correct |
17 |
Correct |
4 ms |
21596 KB |
Output is correct |
18 |
Correct |
6 ms |
22264 KB |
Output is correct |
19 |
Correct |
6 ms |
22228 KB |
Output is correct |
20 |
Correct |
5 ms |
22364 KB |
Output is correct |
21 |
Correct |
5 ms |
22108 KB |
Output is correct |
22 |
Correct |
5 ms |
22208 KB |
Output is correct |
23 |
Correct |
7 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22108 KB |
Output is correct |
25 |
Correct |
3 ms |
21596 KB |
Output is correct |
26 |
Correct |
6 ms |
22108 KB |
Output is correct |
27 |
Correct |
5 ms |
22108 KB |
Output is correct |
28 |
Correct |
5 ms |
22108 KB |
Output is correct |
29 |
Correct |
5 ms |
22108 KB |
Output is correct |
30 |
Correct |
6 ms |
21852 KB |
Output is correct |
31 |
Correct |
5 ms |
21852 KB |
Output is correct |
32 |
Correct |
5 ms |
22032 KB |
Output is correct |
33 |
Correct |
6 ms |
21852 KB |
Output is correct |
34 |
Correct |
88 ms |
41044 KB |
Output is correct |
35 |
Correct |
77 ms |
47396 KB |
Output is correct |
36 |
Correct |
81 ms |
47140 KB |
Output is correct |
37 |
Correct |
118 ms |
46488 KB |
Output is correct |
38 |
Correct |
104 ms |
41044 KB |
Output is correct |
39 |
Correct |
102 ms |
44996 KB |
Output is correct |
40 |
Correct |
101 ms |
44164 KB |
Output is correct |
41 |
Correct |
4 ms |
21592 KB |
Output is correct |
42 |
Correct |
4 ms |
21596 KB |
Output is correct |
43 |
Correct |
124 ms |
34836 KB |
Output is correct |
44 |
Correct |
111 ms |
34684 KB |
Output is correct |
45 |
Correct |
119 ms |
34856 KB |
Output is correct |
46 |
Correct |
102 ms |
43520 KB |
Output is correct |
47 |
Correct |
88 ms |
43516 KB |
Output is correct |
48 |
Correct |
94 ms |
43500 KB |
Output is correct |
49 |
Correct |
96 ms |
43616 KB |
Output is correct |
50 |
Correct |
136 ms |
34900 KB |
Output is correct |
51 |
Correct |
154 ms |
34968 KB |
Output is correct |
52 |
Correct |
145 ms |
34968 KB |
Output is correct |
53 |
Correct |
148 ms |
34792 KB |
Output is correct |