#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("trapv")
const int MAXN = 2e5 + 25;
const int B = 20;
typedef long long ll;
int a[MAXN], n;
vector <int> adj[MAXN];
int inv[MAXN];
ll dp[B][MAXN], dep[MAXN];
ll ans[MAXN];
struct DSU {
int par[MAXN];
void init () {
for (int i = 1; i <= n; i++) {
par[i] = i;
}
}
int find (int x) {
return par[x] == x ? x : par[x] = find(par[x]);
}
void merge (int x, int y) {
x = find(x); y = find(y);
if (x == y) return;
if (a[x] > a[y]) {
par[y] = x;
} else {
par[x] = y;
}
}
} cur;
int jump (int x, int y) {
for (int i = B - 1; i >= 0; i--) {
if (y & (1 << i)) {
x = dp[i][x];
}
}
return x;
}
int lca (int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
y = jump(y, dep[y] - dep[x]);
if (x == y) return x ;
for (int i = B - 1; i >= 0; i--) {
if (dp[i][x] != dp[i][y]) {
x = dp[i][x], y = dp[i][x];
}
}
return dp[0][x];
}
ll dist (int x, int y) {
return dep[x] + dep[y] - 2 * dep[lca(x, y)];
}
void dfs2 (int pos, int par) {
for (auto j : adj[pos]) {
if (j != par) {
dp[0][j] = pos;
for (int i = 1; i < B; i++) {
dp[i][j] = dp[i - 1][dp[i - 1][j]];
}
dep[j] = 1 + dep[pos];
dfs2(j, pos);
}
}
}
void solve () {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i]; inv[a[i]] = i;
}
for (int i = 1; i < n; i++) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs2(1, 0);
cur.init();
for (int x = 1; x <= n; x++) {
int i = inv[x];
for (auto j : adj[i]) {
if (a[j] > a[i]) {
continue;
}
ans[i] = max(ans[i], ans[cur.find(j)] + dist(cur.find(j), i));
cur.merge(i, cur.find(j));
}
}
cout << ans[inv[n]] << '\n';
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
1 ms |
14940 KB |
Output is correct |
3 |
Correct |
1 ms |
14940 KB |
Output is correct |
4 |
Correct |
1 ms |
16732 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
1 ms |
14940 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
1 ms |
14940 KB |
Output is correct |
3 |
Correct |
1 ms |
14940 KB |
Output is correct |
4 |
Correct |
1 ms |
16732 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
1 ms |
14940 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14936 KB |
Output is correct |
15 |
Correct |
2 ms |
14776 KB |
Output is correct |
16 |
Correct |
1 ms |
14940 KB |
Output is correct |
17 |
Correct |
2 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
1 ms |
14940 KB |
Output is correct |
3 |
Correct |
1 ms |
14940 KB |
Output is correct |
4 |
Correct |
1 ms |
16732 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
1 ms |
14940 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14936 KB |
Output is correct |
15 |
Correct |
2 ms |
14776 KB |
Output is correct |
16 |
Correct |
1 ms |
14940 KB |
Output is correct |
17 |
Correct |
2 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
16220 KB |
Output is correct |
19 |
Correct |
3 ms |
16220 KB |
Output is correct |
20 |
Correct |
4 ms |
16216 KB |
Output is correct |
21 |
Correct |
4 ms |
16056 KB |
Output is correct |
22 |
Correct |
3 ms |
12124 KB |
Output is correct |
23 |
Correct |
3 ms |
16220 KB |
Output is correct |
24 |
Correct |
3 ms |
16180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
1 ms |
14940 KB |
Output is correct |
3 |
Correct |
1 ms |
14940 KB |
Output is correct |
4 |
Correct |
1 ms |
16732 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
1 ms |
14940 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14936 KB |
Output is correct |
15 |
Correct |
2 ms |
14776 KB |
Output is correct |
16 |
Correct |
1 ms |
14940 KB |
Output is correct |
17 |
Correct |
2 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
16220 KB |
Output is correct |
19 |
Correct |
3 ms |
16220 KB |
Output is correct |
20 |
Correct |
4 ms |
16216 KB |
Output is correct |
21 |
Correct |
4 ms |
16056 KB |
Output is correct |
22 |
Correct |
3 ms |
12124 KB |
Output is correct |
23 |
Correct |
3 ms |
16220 KB |
Output is correct |
24 |
Correct |
3 ms |
16180 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
4 ms |
15964 KB |
Output is correct |
27 |
Incorrect |
4 ms |
15964 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
1 ms |
14940 KB |
Output is correct |
3 |
Correct |
1 ms |
14940 KB |
Output is correct |
4 |
Correct |
1 ms |
16732 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
1 ms |
14940 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14936 KB |
Output is correct |
15 |
Correct |
2 ms |
14776 KB |
Output is correct |
16 |
Correct |
1 ms |
14940 KB |
Output is correct |
17 |
Correct |
2 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
16220 KB |
Output is correct |
19 |
Correct |
3 ms |
16220 KB |
Output is correct |
20 |
Correct |
4 ms |
16216 KB |
Output is correct |
21 |
Correct |
4 ms |
16056 KB |
Output is correct |
22 |
Correct |
3 ms |
12124 KB |
Output is correct |
23 |
Correct |
3 ms |
16220 KB |
Output is correct |
24 |
Correct |
3 ms |
16180 KB |
Output is correct |
25 |
Correct |
84 ms |
69888 KB |
Output is correct |
26 |
Correct |
86 ms |
69964 KB |
Output is correct |
27 |
Correct |
90 ms |
70108 KB |
Output is correct |
28 |
Correct |
110 ms |
70020 KB |
Output is correct |
29 |
Correct |
107 ms |
69972 KB |
Output is correct |
30 |
Correct |
107 ms |
69864 KB |
Output is correct |
31 |
Correct |
113 ms |
69916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Incorrect |
2 ms |
14940 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
1 ms |
14940 KB |
Output is correct |
3 |
Correct |
1 ms |
14940 KB |
Output is correct |
4 |
Correct |
1 ms |
16732 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
1 ms |
14684 KB |
Output is correct |
7 |
Correct |
1 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
1 ms |
14940 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
14940 KB |
Output is correct |
12 |
Correct |
2 ms |
14940 KB |
Output is correct |
13 |
Correct |
2 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14936 KB |
Output is correct |
15 |
Correct |
2 ms |
14776 KB |
Output is correct |
16 |
Correct |
1 ms |
14940 KB |
Output is correct |
17 |
Correct |
2 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
16220 KB |
Output is correct |
19 |
Correct |
3 ms |
16220 KB |
Output is correct |
20 |
Correct |
4 ms |
16216 KB |
Output is correct |
21 |
Correct |
4 ms |
16056 KB |
Output is correct |
22 |
Correct |
3 ms |
12124 KB |
Output is correct |
23 |
Correct |
3 ms |
16220 KB |
Output is correct |
24 |
Correct |
3 ms |
16180 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
4 ms |
15964 KB |
Output is correct |
27 |
Incorrect |
4 ms |
15964 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |