#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> r, p, h;
DSU() {}
DSU(int n) : r(n), p(n), h(n) {
for (int i = 0; i < n; ++i) p[i] = h[i] = i;
}
int find(int a) { return a == p[a] ? a : p[a] = find(p[a]); }
void link(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
if (r[a] < r[b]) swap(a, b);
if (r[a] == r[b]) ++r[a];
p[b] = a, h[a] = max(h[a], h[b]);
}
};
struct LCA {
vector<vector<int>> adj;
vector<int> depths;
int n;
vector<vector<int>> up;
LCA() {}
LCA(int n, vector<vector<int>> _adj) : n(n) {
adj = _adj;
depths.resize(n);
up = vector<vector<int>>(n, vector<int>(20));
}
void dfs(int v, int p, int d) {
depths[v] = d, up[v][0] = p;
for (int u : adj[v]) if (u != p) dfs(u, v, d + 1);
}
void init() {
dfs(0, 0, 0);
for (int j = 1; j < 20; ++j) for (int i = 0; i < n; ++i) up[i][j] = up[up[i][j - 1]][j - 1];
}
int jmp(int a, int u) {
for (int i = 0; i < 20; ++i) if (u & (1 << i)) a = up[a][i];
return a;
}
int lca(int a, int b) {
if (depths[a] > depths[b]) swap(a, b);
b = jmp(b, depths[b] - depths[a]);
for (int i = 19; i >= 0; --i) if (up[a][i] != up[b][i]) a = up[a][i], b = up[b][i];
return a != b ? up[a][0] : a;
}
int dist(int a, int b) {
// cout << a << ' ' << b << ' ' << lca(a, b) << ' ' << depths[a] << ' ' << depths[b] << ' ' << 2 * depths[lca(a, b)] << endl;
return depths[a] + depths[b] - 2 * depths[lca(a, b)];
}
};
int main() {
int n; cin >> n;
vector<int> h(n); for (int i = 0; i < n; ++i) cin >> h[i];
for (int i = 0; i < n; ++i) --h[i];
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; ++i) {
int a, b; cin >> a >> b; --a, --b;
a = h[a], b = h[b];
adj[a].push_back(b); adj[b].push_back(a);
}
DSU dsu(n);
LCA lca(n, adj); lca.init();
vector<long long> dp(n);
for (int i = 0; i < n; ++i) {
// cout << i << endl;
for (int u : adj[i]) {
if (u > i) continue;
int ru = dsu.h[dsu.find(u)];
// cout << i << ' ' << ru << ' ' << lca.dist(i, ru) << '\n';
dp[i] = max(dp[i], lca.dist(i, ru) + dp[ru]);
dsu.link(i, ru);
}
}
// for (int num : dp) cout << num << ' ';
// cout << '\n';
cout << dp[n - 1] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
388 KB |
Output is correct |
18 |
Correct |
5 ms |
1748 KB |
Output is correct |
19 |
Correct |
5 ms |
1876 KB |
Output is correct |
20 |
Correct |
5 ms |
1876 KB |
Output is correct |
21 |
Correct |
5 ms |
1748 KB |
Output is correct |
22 |
Correct |
5 ms |
1876 KB |
Output is correct |
23 |
Correct |
6 ms |
1876 KB |
Output is correct |
24 |
Correct |
7 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
388 KB |
Output is correct |
18 |
Correct |
5 ms |
1748 KB |
Output is correct |
19 |
Correct |
5 ms |
1876 KB |
Output is correct |
20 |
Correct |
5 ms |
1876 KB |
Output is correct |
21 |
Correct |
5 ms |
1748 KB |
Output is correct |
22 |
Correct |
5 ms |
1876 KB |
Output is correct |
23 |
Correct |
6 ms |
1876 KB |
Output is correct |
24 |
Correct |
7 ms |
1876 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
7 ms |
1876 KB |
Output is correct |
27 |
Correct |
6 ms |
1876 KB |
Output is correct |
28 |
Correct |
5 ms |
1876 KB |
Output is correct |
29 |
Correct |
6 ms |
1856 KB |
Output is correct |
30 |
Correct |
5 ms |
1748 KB |
Output is correct |
31 |
Correct |
6 ms |
1748 KB |
Output is correct |
32 |
Correct |
6 ms |
1748 KB |
Output is correct |
33 |
Correct |
5 ms |
1748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
388 KB |
Output is correct |
18 |
Correct |
5 ms |
1748 KB |
Output is correct |
19 |
Correct |
5 ms |
1876 KB |
Output is correct |
20 |
Correct |
5 ms |
1876 KB |
Output is correct |
21 |
Correct |
5 ms |
1748 KB |
Output is correct |
22 |
Correct |
5 ms |
1876 KB |
Output is correct |
23 |
Correct |
6 ms |
1876 KB |
Output is correct |
24 |
Correct |
7 ms |
1876 KB |
Output is correct |
25 |
Correct |
230 ms |
60456 KB |
Output is correct |
26 |
Correct |
273 ms |
61084 KB |
Output is correct |
27 |
Correct |
250 ms |
61104 KB |
Output is correct |
28 |
Correct |
396 ms |
60772 KB |
Output is correct |
29 |
Correct |
393 ms |
60472 KB |
Output is correct |
30 |
Correct |
371 ms |
60492 KB |
Output is correct |
31 |
Correct |
375 ms |
60476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
329 ms |
60460 KB |
Output is correct |
4 |
Correct |
327 ms |
60524 KB |
Output is correct |
5 |
Correct |
334 ms |
60460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
388 KB |
Output is correct |
18 |
Correct |
5 ms |
1748 KB |
Output is correct |
19 |
Correct |
5 ms |
1876 KB |
Output is correct |
20 |
Correct |
5 ms |
1876 KB |
Output is correct |
21 |
Correct |
5 ms |
1748 KB |
Output is correct |
22 |
Correct |
5 ms |
1876 KB |
Output is correct |
23 |
Correct |
6 ms |
1876 KB |
Output is correct |
24 |
Correct |
7 ms |
1876 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
7 ms |
1876 KB |
Output is correct |
27 |
Correct |
6 ms |
1876 KB |
Output is correct |
28 |
Correct |
5 ms |
1876 KB |
Output is correct |
29 |
Correct |
6 ms |
1856 KB |
Output is correct |
30 |
Correct |
5 ms |
1748 KB |
Output is correct |
31 |
Correct |
6 ms |
1748 KB |
Output is correct |
32 |
Correct |
6 ms |
1748 KB |
Output is correct |
33 |
Correct |
5 ms |
1748 KB |
Output is correct |
34 |
Correct |
230 ms |
60456 KB |
Output is correct |
35 |
Correct |
273 ms |
61084 KB |
Output is correct |
36 |
Correct |
250 ms |
61104 KB |
Output is correct |
37 |
Correct |
396 ms |
60772 KB |
Output is correct |
38 |
Correct |
393 ms |
60472 KB |
Output is correct |
39 |
Correct |
371 ms |
60492 KB |
Output is correct |
40 |
Correct |
375 ms |
60476 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
329 ms |
60460 KB |
Output is correct |
44 |
Correct |
327 ms |
60524 KB |
Output is correct |
45 |
Correct |
334 ms |
60460 KB |
Output is correct |
46 |
Correct |
269 ms |
60480 KB |
Output is correct |
47 |
Correct |
281 ms |
60476 KB |
Output is correct |
48 |
Correct |
268 ms |
60512 KB |
Output is correct |
49 |
Correct |
264 ms |
60468 KB |
Output is correct |
50 |
Correct |
368 ms |
60556 KB |
Output is correct |
51 |
Correct |
384 ms |
60588 KB |
Output is correct |
52 |
Correct |
376 ms |
60596 KB |
Output is correct |
53 |
Correct |
371 ms |
60620 KB |
Output is correct |