#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int N; cin >> N;
vector<int> P(N + 1), revP(N + 1);
for(int i = 1; i <= N; i++) cin >> P[i], revP[P[i]] = i;
vector<vector<int>> adj(N + 1);
for(int i = 0; i < N - 1; i++) {
int A, B; cin >> A >> B;
adj[A].push_back(B);
adj[B].push_back(A);
}
vector<vector<int>> jmp(N + 1, vector<int>(20));
vector<int> dep(N + 1);
function<void(int, int)> dfs = [&] (int s, int e) {
jmp[s][0] = e;
dep[s] = dep[e] + 1;
for(int i = 0; i < 19; i++) {
jmp[s][i + 1] = jmp[jmp[s][i]][i];
}
for(int u : adj[s]) if(u != e) dfs(u, s);
};
dfs(1, 0);
auto lca = [&] (int a, int b) {
if(dep[a] < dep[b]) swap(a, b);
for(int i = 19; i >= 0; i--) {
if(dep[jmp[a][i]] >= dep[b])
a = jmp[a][i];
}
if(a == b) return a;
for(int i = 19; i >= 0; i--) {
if(jmp[a][i] != jmp[b][i]) {
a = jmp[a][i];
b = jmp[b][i];
}
}
return jmp[a][0];
};
auto dist = [&] (int s, int t) {
return dep[s] + dep[t] - 2 * dep[lca(s, t)];
};
vector<int> e(N + 1, -1);
function<int(int)> get = [&] (int s) {
return e[s] < 0 ? s : (e[s] = get(e[s]));
};
vector<ll> dp(N + 1);
for(int i = 1; i <= N; i++) {
int s = revP[i];
for(int u : adj[s]) {
if(P[u] <= i) {
int g = get(u);
dp[s] = max(dp[s], dist(s, g) + dp[g]);
e[s] += e[g];
e[g] = s;
}
}
}
cout << dp[revP[N]] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
1628 KB |
Output is correct |
19 |
Correct |
5 ms |
1760 KB |
Output is correct |
20 |
Correct |
5 ms |
1628 KB |
Output is correct |
21 |
Correct |
5 ms |
1628 KB |
Output is correct |
22 |
Correct |
5 ms |
1628 KB |
Output is correct |
23 |
Correct |
5 ms |
1628 KB |
Output is correct |
24 |
Correct |
5 ms |
1624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
1628 KB |
Output is correct |
19 |
Correct |
5 ms |
1760 KB |
Output is correct |
20 |
Correct |
5 ms |
1628 KB |
Output is correct |
21 |
Correct |
5 ms |
1628 KB |
Output is correct |
22 |
Correct |
5 ms |
1628 KB |
Output is correct |
23 |
Correct |
5 ms |
1628 KB |
Output is correct |
24 |
Correct |
5 ms |
1624 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
6 ms |
1484 KB |
Output is correct |
27 |
Correct |
5 ms |
1628 KB |
Output is correct |
28 |
Correct |
6 ms |
1368 KB |
Output is correct |
29 |
Correct |
6 ms |
1628 KB |
Output is correct |
30 |
Correct |
5 ms |
1372 KB |
Output is correct |
31 |
Correct |
5 ms |
1236 KB |
Output is correct |
32 |
Correct |
7 ms |
1372 KB |
Output is correct |
33 |
Correct |
5 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
1628 KB |
Output is correct |
19 |
Correct |
5 ms |
1760 KB |
Output is correct |
20 |
Correct |
5 ms |
1628 KB |
Output is correct |
21 |
Correct |
5 ms |
1628 KB |
Output is correct |
22 |
Correct |
5 ms |
1628 KB |
Output is correct |
23 |
Correct |
5 ms |
1628 KB |
Output is correct |
24 |
Correct |
5 ms |
1624 KB |
Output is correct |
25 |
Correct |
227 ms |
55140 KB |
Output is correct |
26 |
Correct |
231 ms |
58824 KB |
Output is correct |
27 |
Correct |
233 ms |
58936 KB |
Output is correct |
28 |
Correct |
295 ms |
58960 KB |
Output is correct |
29 |
Correct |
296 ms |
58960 KB |
Output is correct |
30 |
Correct |
329 ms |
58912 KB |
Output is correct |
31 |
Correct |
294 ms |
58824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
290 ms |
39384 KB |
Output is correct |
4 |
Correct |
311 ms |
39388 KB |
Output is correct |
5 |
Correct |
287 ms |
39636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
500 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
5 ms |
1628 KB |
Output is correct |
19 |
Correct |
5 ms |
1760 KB |
Output is correct |
20 |
Correct |
5 ms |
1628 KB |
Output is correct |
21 |
Correct |
5 ms |
1628 KB |
Output is correct |
22 |
Correct |
5 ms |
1628 KB |
Output is correct |
23 |
Correct |
5 ms |
1628 KB |
Output is correct |
24 |
Correct |
5 ms |
1624 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
6 ms |
1484 KB |
Output is correct |
27 |
Correct |
5 ms |
1628 KB |
Output is correct |
28 |
Correct |
6 ms |
1368 KB |
Output is correct |
29 |
Correct |
6 ms |
1628 KB |
Output is correct |
30 |
Correct |
5 ms |
1372 KB |
Output is correct |
31 |
Correct |
5 ms |
1236 KB |
Output is correct |
32 |
Correct |
7 ms |
1372 KB |
Output is correct |
33 |
Correct |
5 ms |
1372 KB |
Output is correct |
34 |
Correct |
227 ms |
55140 KB |
Output is correct |
35 |
Correct |
231 ms |
58824 KB |
Output is correct |
36 |
Correct |
233 ms |
58936 KB |
Output is correct |
37 |
Correct |
295 ms |
58960 KB |
Output is correct |
38 |
Correct |
296 ms |
58960 KB |
Output is correct |
39 |
Correct |
329 ms |
58912 KB |
Output is correct |
40 |
Correct |
294 ms |
58824 KB |
Output is correct |
41 |
Correct |
0 ms |
344 KB |
Output is correct |
42 |
Correct |
0 ms |
600 KB |
Output is correct |
43 |
Correct |
290 ms |
39384 KB |
Output is correct |
44 |
Correct |
311 ms |
39388 KB |
Output is correct |
45 |
Correct |
287 ms |
39636 KB |
Output is correct |
46 |
Correct |
552 ms |
51992 KB |
Output is correct |
47 |
Correct |
467 ms |
50512 KB |
Output is correct |
48 |
Correct |
492 ms |
52676 KB |
Output is correct |
49 |
Correct |
503 ms |
50628 KB |
Output is correct |
50 |
Correct |
436 ms |
43556 KB |
Output is correct |
51 |
Correct |
407 ms |
43332 KB |
Output is correct |
52 |
Correct |
424 ms |
43472 KB |
Output is correct |
53 |
Correct |
411 ms |
43720 KB |
Output is correct |