#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
struct dsu {
vector<int> p;
dsu(int n) : p(n+1) {
iota(all(p), 0);
}
int get(int v) {
return v == p[v] ? v : p[v] = get(p[v]);
}
void merge(int a, int b) {
a = get(a), b = get(b);
if (a != b) p[b] = a;
}
};
const int LOG = 18;
void test_case() {
int n; cin >> n;
vector<int> h(n+1);
for (int i = 1; i <= n; i++) cin >> h[i];
vector<vector<int>> adj(n+1);
for (int i = 0; i < n-1; i++) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<vector<int>> up(n+1, vector<int>(LOG));
vector<int> depth(n+1);
auto dfs = [&](auto&& s, int v, int p) -> void {
for (int u : adj[v]) if (u != p) {
depth[u] = depth[v] + 1;
up[u][0] = v;
for (int i = 1; i < LOG; i++) {
up[u][i] = up[up[u][i-1]][i-1];
}
s(s, u, v);
}
};
dfs(dfs, 1, -1);
auto lca = [&](int x, int y) {
if (depth[x] < depth[y]) swap(x, y);
int k = depth[x] - depth[y];
for (int i = 0; i < LOG; i++) if (k >> i & 1) x = up[x][i];
for (int i = LOG-1; i >= 0; i--) if (up[x][i] != up[y][i]) x = up[x][i], y = up[y][i];
return x == y ? x : up[x][0];
};
auto dist = [&](int x, int y) {
return depth[x] + depth[y] - 2 * depth[lca(x, y)];
};
vector<int> order(n);
iota(all(order), 1);
sort(all(order), [&](int x, int y) { return h[x] < h[y]; });
vector<ll> dp(n+1, -1e18);
dsu d(n);
for (int v : order) {
dp[v] = 0;
for (int u : adj[v]) {
u = d.get(u);
if (h[v] > h[u]) {
ckmax(dp[v], dp[u] + dist(u, v));
d.merge(v, u);
}
}
}
cout << *max_element(all(dp)) << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
3 ms |
1492 KB |
Output is correct |
19 |
Correct |
3 ms |
1368 KB |
Output is correct |
20 |
Correct |
3 ms |
1512 KB |
Output is correct |
21 |
Correct |
4 ms |
1368 KB |
Output is correct |
22 |
Correct |
4 ms |
1368 KB |
Output is correct |
23 |
Correct |
4 ms |
1368 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
3 ms |
1492 KB |
Output is correct |
19 |
Correct |
3 ms |
1368 KB |
Output is correct |
20 |
Correct |
3 ms |
1512 KB |
Output is correct |
21 |
Correct |
4 ms |
1368 KB |
Output is correct |
22 |
Correct |
4 ms |
1368 KB |
Output is correct |
23 |
Correct |
4 ms |
1368 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
1372 KB |
Output is correct |
27 |
Correct |
4 ms |
1368 KB |
Output is correct |
28 |
Correct |
4 ms |
1368 KB |
Output is correct |
29 |
Correct |
4 ms |
1372 KB |
Output is correct |
30 |
Correct |
6 ms |
1372 KB |
Output is correct |
31 |
Correct |
4 ms |
1364 KB |
Output is correct |
32 |
Correct |
6 ms |
1356 KB |
Output is correct |
33 |
Correct |
4 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
3 ms |
1492 KB |
Output is correct |
19 |
Correct |
3 ms |
1368 KB |
Output is correct |
20 |
Correct |
3 ms |
1512 KB |
Output is correct |
21 |
Correct |
4 ms |
1368 KB |
Output is correct |
22 |
Correct |
4 ms |
1368 KB |
Output is correct |
23 |
Correct |
4 ms |
1368 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
149 ms |
46672 KB |
Output is correct |
26 |
Correct |
141 ms |
46840 KB |
Output is correct |
27 |
Correct |
132 ms |
46856 KB |
Output is correct |
28 |
Correct |
187 ms |
46708 KB |
Output is correct |
29 |
Correct |
207 ms |
46852 KB |
Output is correct |
30 |
Correct |
191 ms |
46676 KB |
Output is correct |
31 |
Correct |
192 ms |
46812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
219 ms |
40136 KB |
Output is correct |
4 |
Correct |
220 ms |
40332 KB |
Output is correct |
5 |
Correct |
237 ms |
40136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
456 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
3 ms |
1492 KB |
Output is correct |
19 |
Correct |
3 ms |
1368 KB |
Output is correct |
20 |
Correct |
3 ms |
1512 KB |
Output is correct |
21 |
Correct |
4 ms |
1368 KB |
Output is correct |
22 |
Correct |
4 ms |
1368 KB |
Output is correct |
23 |
Correct |
4 ms |
1368 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
1372 KB |
Output is correct |
27 |
Correct |
4 ms |
1368 KB |
Output is correct |
28 |
Correct |
4 ms |
1368 KB |
Output is correct |
29 |
Correct |
4 ms |
1372 KB |
Output is correct |
30 |
Correct |
6 ms |
1372 KB |
Output is correct |
31 |
Correct |
4 ms |
1364 KB |
Output is correct |
32 |
Correct |
6 ms |
1356 KB |
Output is correct |
33 |
Correct |
4 ms |
1372 KB |
Output is correct |
34 |
Correct |
149 ms |
46672 KB |
Output is correct |
35 |
Correct |
141 ms |
46840 KB |
Output is correct |
36 |
Correct |
132 ms |
46856 KB |
Output is correct |
37 |
Correct |
187 ms |
46708 KB |
Output is correct |
38 |
Correct |
207 ms |
46852 KB |
Output is correct |
39 |
Correct |
191 ms |
46676 KB |
Output is correct |
40 |
Correct |
192 ms |
46812 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
219 ms |
40136 KB |
Output is correct |
44 |
Correct |
220 ms |
40332 KB |
Output is correct |
45 |
Correct |
237 ms |
40136 KB |
Output is correct |
46 |
Correct |
395 ms |
43900 KB |
Output is correct |
47 |
Correct |
406 ms |
43316 KB |
Output is correct |
48 |
Correct |
363 ms |
44184 KB |
Output is correct |
49 |
Correct |
347 ms |
43316 KB |
Output is correct |
50 |
Correct |
367 ms |
40360 KB |
Output is correct |
51 |
Correct |
345 ms |
40352 KB |
Output is correct |
52 |
Correct |
332 ms |
40352 KB |
Output is correct |
53 |
Correct |
319 ms |
40276 KB |
Output is correct |