/**
* author: wxhtzdy
* created: 12.02.2023 13:22:57
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
const int L = 25;
vector<vector<int>> jump(n, vector<int>(L));
vector<int> dep(n);
function<void(int, int)> Dfs = [&](int v, int pv) {
dep[v] = dep[pv] + 1;
jump[v][0] = pv;
for (int j = 1; j < L; j++) {
jump[v][j] = jump[jump[v][j - 1]][j - 1];
}
for (int u : g[v]) {
if (u == pv) {
continue;
}
Dfs(u, v);
}
};
Dfs(0, 0);
auto LCA = [&](int v, int u) {
if (dep[v] > dep[u]) {
swap(v, u);
}
for (int j = L - 1; j >= 0; j--) {
if (dep[jump[u][j]] >= dep[v]) {
u = jump[u][j];
}
}
if (v == u) {
return v;
}
for (int j = L - 1; j >= 0; j--) {
if (jump[v][j] != jump[u][j]) {
v = jump[v][j];
u = jump[u][j];
}
}
return jump[v][0];
};
auto GetDist = [&](int v, int u) {
return dep[v] + dep[u] - 2 * dep[LCA(v, u)];
};
vector<int> par(n);
iota(par.begin(), par.end(), 0);
function<int(int)> get = [&](int x) {
return par[x] == x ? x : par[x] = get(par[x]);
};
vector<int> order(n);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int i, int j) {
return h[i] < h[j];
});
vector<long long> dp(n);
for (int i : order) {
for (int j : g[i]) {
if (h[i] > h[j]) {
dp[i] = max(dp[i], dp[get(j)] + GetDist(i, get(j)));
par[get(j)] = i;
}
}
}
cout << dp[order[n - 1]] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
340 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
340 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 |
340 KB |
Output is correct |
18 |
Correct |
6 ms |
1812 KB |
Output is correct |
19 |
Correct |
6 ms |
1756 KB |
Output is correct |
20 |
Correct |
6 ms |
1812 KB |
Output is correct |
21 |
Correct |
7 ms |
1748 KB |
Output is correct |
22 |
Correct |
6 ms |
1772 KB |
Output is correct |
23 |
Correct |
6 ms |
1748 KB |
Output is correct |
24 |
Correct |
7 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
340 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 |
340 KB |
Output is correct |
18 |
Correct |
6 ms |
1812 KB |
Output is correct |
19 |
Correct |
6 ms |
1756 KB |
Output is correct |
20 |
Correct |
6 ms |
1812 KB |
Output is correct |
21 |
Correct |
7 ms |
1748 KB |
Output is correct |
22 |
Correct |
6 ms |
1772 KB |
Output is correct |
23 |
Correct |
6 ms |
1748 KB |
Output is correct |
24 |
Correct |
7 ms |
1748 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
8 ms |
1540 KB |
Output is correct |
27 |
Correct |
7 ms |
1584 KB |
Output is correct |
28 |
Correct |
7 ms |
1528 KB |
Output is correct |
29 |
Correct |
6 ms |
1632 KB |
Output is correct |
30 |
Correct |
6 ms |
1376 KB |
Output is correct |
31 |
Correct |
8 ms |
1388 KB |
Output is correct |
32 |
Correct |
7 ms |
1432 KB |
Output is correct |
33 |
Correct |
8 ms |
1500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
340 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 |
340 KB |
Output is correct |
18 |
Correct |
6 ms |
1812 KB |
Output is correct |
19 |
Correct |
6 ms |
1756 KB |
Output is correct |
20 |
Correct |
6 ms |
1812 KB |
Output is correct |
21 |
Correct |
7 ms |
1748 KB |
Output is correct |
22 |
Correct |
6 ms |
1772 KB |
Output is correct |
23 |
Correct |
6 ms |
1748 KB |
Output is correct |
24 |
Correct |
7 ms |
1748 KB |
Output is correct |
25 |
Correct |
237 ms |
59268 KB |
Output is correct |
26 |
Correct |
231 ms |
59432 KB |
Output is correct |
27 |
Correct |
245 ms |
60808 KB |
Output is correct |
28 |
Correct |
339 ms |
58676 KB |
Output is correct |
29 |
Correct |
355 ms |
58916 KB |
Output is correct |
30 |
Correct |
311 ms |
60708 KB |
Output is correct |
31 |
Correct |
305 ms |
58688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
310 ms |
45400 KB |
Output is correct |
4 |
Correct |
304 ms |
43008 KB |
Output is correct |
5 |
Correct |
300 ms |
45380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
340 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 |
340 KB |
Output is correct |
18 |
Correct |
6 ms |
1812 KB |
Output is correct |
19 |
Correct |
6 ms |
1756 KB |
Output is correct |
20 |
Correct |
6 ms |
1812 KB |
Output is correct |
21 |
Correct |
7 ms |
1748 KB |
Output is correct |
22 |
Correct |
6 ms |
1772 KB |
Output is correct |
23 |
Correct |
6 ms |
1748 KB |
Output is correct |
24 |
Correct |
7 ms |
1748 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
8 ms |
1540 KB |
Output is correct |
27 |
Correct |
7 ms |
1584 KB |
Output is correct |
28 |
Correct |
7 ms |
1528 KB |
Output is correct |
29 |
Correct |
6 ms |
1632 KB |
Output is correct |
30 |
Correct |
6 ms |
1376 KB |
Output is correct |
31 |
Correct |
8 ms |
1388 KB |
Output is correct |
32 |
Correct |
7 ms |
1432 KB |
Output is correct |
33 |
Correct |
8 ms |
1500 KB |
Output is correct |
34 |
Correct |
237 ms |
59268 KB |
Output is correct |
35 |
Correct |
231 ms |
59432 KB |
Output is correct |
36 |
Correct |
245 ms |
60808 KB |
Output is correct |
37 |
Correct |
339 ms |
58676 KB |
Output is correct |
38 |
Correct |
355 ms |
58916 KB |
Output is correct |
39 |
Correct |
311 ms |
60708 KB |
Output is correct |
40 |
Correct |
305 ms |
58688 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
310 ms |
45400 KB |
Output is correct |
44 |
Correct |
304 ms |
43008 KB |
Output is correct |
45 |
Correct |
300 ms |
45380 KB |
Output is correct |
46 |
Correct |
625 ms |
51952 KB |
Output is correct |
47 |
Correct |
599 ms |
50008 KB |
Output is correct |
48 |
Correct |
626 ms |
52920 KB |
Output is correct |
49 |
Correct |
644 ms |
51632 KB |
Output is correct |
50 |
Correct |
441 ms |
45016 KB |
Output is correct |
51 |
Correct |
439 ms |
43940 KB |
Output is correct |
52 |
Correct |
458 ms |
42928 KB |
Output is correct |
53 |
Correct |
454 ms |
42784 KB |
Output is correct |