# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define ll long long
#define pii pair <int, int>
#define pb push_back
const int N = 3e5 + 5;
int n, a[N], lv[N], par[N][20],tin, in[N], out[N], p[N], mx[N], inv[N];
vector <int> v[N];
ll dp[N];
void dfs(int a, int p) {
par[a][0] = p;
in[a] = ++tin;
lv[a] = lv[p] + 1;
for (int i = 1; i <= 18; i++) {
par[a][i] = par[par[a][i - 1]][i - 1];
}
for (int to : v[a]) {
if (to == p) continue;
dfs(to, a);
}
out[a] = ++tin;
}
int upper(int a, int b) {
return (in[a] <= in[b] && out[a] >= out[b]);
}
int lca(int a, int b) {
if (upper(a, b)) return a;
for (int i = 18; i >= 0; i--) {
if (par[a][i] && !upper(par[a][i], b)) a = par[a][i];
}
return par[a][0];
}
int dist(int a, int b) {
return lv[a] + lv[b] - 2 * lv[lca(a, b)];
}
int get_col(int a) {
if (a == p[a]) return a;
else return p[a] = get_col(p[a]);
}
void col(int a, int b) {
a = get_col(a);
b = get_col(b);
if (a == b) return ;
p[b] = a;
mx[a] = max(mx[a], mx[b]);
}
void init() {
for (int i = 1; i <= n ;i++) {
p[i] = i;
mx[i] = a[i];
}
}
signed main() {
cin>>n;
for (int i = 1; i <= n; i++) {
cin>>a[i];
inv[a[i]] = i;
}
for (int i = 1; i <= n - 1; i++) {
int a, b;
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
init();
dfs(1, 0);
for (int i = 1; i <= n; i++) {
int vert = inv[i];
for (int to : v[vert]) {
if (a[to] > i) continue;
int nxt_vert = get_col(to); // maximum in the "subtree"
dp[vert] = max(dp[vert], dp[nxt_vert] + dist(nxt_vert, vert));
// cout<<vert <<" -- > "<<to<<" "<<nxt_vert<<" "<<dist(nxt_vert, vert)<<" "<<dp[vert]<<"\n";
}
for (int to : v[vert]) {
if (a[to] > i) continue;
col(vert, to);
}
}
cout<<dp[inv[n]]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7348 KB |
Output is correct |
3 |
Correct |
3 ms |
7360 KB |
Output is correct |
4 |
Correct |
3 ms |
7288 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7304 KB |
Output is correct |
8 |
Correct |
3 ms |
7364 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7348 KB |
Output is correct |
3 |
Correct |
3 ms |
7360 KB |
Output is correct |
4 |
Correct |
3 ms |
7288 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7304 KB |
Output is correct |
8 |
Correct |
3 ms |
7364 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7360 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7364 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7348 KB |
Output is correct |
3 |
Correct |
3 ms |
7360 KB |
Output is correct |
4 |
Correct |
3 ms |
7288 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7304 KB |
Output is correct |
8 |
Correct |
3 ms |
7364 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7360 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7364 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
8 ms |
8396 KB |
Output is correct |
19 |
Correct |
8 ms |
8360 KB |
Output is correct |
20 |
Correct |
8 ms |
8404 KB |
Output is correct |
21 |
Correct |
8 ms |
8404 KB |
Output is correct |
22 |
Correct |
8 ms |
8404 KB |
Output is correct |
23 |
Correct |
11 ms |
8396 KB |
Output is correct |
24 |
Correct |
7 ms |
8392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7348 KB |
Output is correct |
3 |
Correct |
3 ms |
7360 KB |
Output is correct |
4 |
Correct |
3 ms |
7288 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7304 KB |
Output is correct |
8 |
Correct |
3 ms |
7364 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7360 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7364 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
8 ms |
8396 KB |
Output is correct |
19 |
Correct |
8 ms |
8360 KB |
Output is correct |
20 |
Correct |
8 ms |
8404 KB |
Output is correct |
21 |
Correct |
8 ms |
8404 KB |
Output is correct |
22 |
Correct |
8 ms |
8404 KB |
Output is correct |
23 |
Correct |
11 ms |
8396 KB |
Output is correct |
24 |
Correct |
7 ms |
8392 KB |
Output is correct |
25 |
Correct |
5 ms |
7360 KB |
Output is correct |
26 |
Correct |
11 ms |
8268 KB |
Output is correct |
27 |
Correct |
8 ms |
8276 KB |
Output is correct |
28 |
Correct |
7 ms |
8232 KB |
Output is correct |
29 |
Correct |
8 ms |
8276 KB |
Output is correct |
30 |
Correct |
8 ms |
8144 KB |
Output is correct |
31 |
Correct |
8 ms |
8140 KB |
Output is correct |
32 |
Correct |
8 ms |
8104 KB |
Output is correct |
33 |
Correct |
8 ms |
8132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7348 KB |
Output is correct |
3 |
Correct |
3 ms |
7360 KB |
Output is correct |
4 |
Correct |
3 ms |
7288 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7304 KB |
Output is correct |
8 |
Correct |
3 ms |
7364 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7360 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7364 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
8 ms |
8396 KB |
Output is correct |
19 |
Correct |
8 ms |
8360 KB |
Output is correct |
20 |
Correct |
8 ms |
8404 KB |
Output is correct |
21 |
Correct |
8 ms |
8404 KB |
Output is correct |
22 |
Correct |
8 ms |
8404 KB |
Output is correct |
23 |
Correct |
11 ms |
8396 KB |
Output is correct |
24 |
Correct |
7 ms |
8392 KB |
Output is correct |
25 |
Correct |
204 ms |
49452 KB |
Output is correct |
26 |
Correct |
219 ms |
49400 KB |
Output is correct |
27 |
Correct |
211 ms |
49460 KB |
Output is correct |
28 |
Correct |
269 ms |
49468 KB |
Output is correct |
29 |
Correct |
264 ms |
49428 KB |
Output is correct |
30 |
Correct |
293 ms |
49388 KB |
Output is correct |
31 |
Correct |
295 ms |
49476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7360 KB |
Output is correct |
2 |
Correct |
4 ms |
7360 KB |
Output is correct |
3 |
Correct |
257 ms |
39988 KB |
Output is correct |
4 |
Correct |
298 ms |
39984 KB |
Output is correct |
5 |
Correct |
260 ms |
39980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
3 ms |
7348 KB |
Output is correct |
3 |
Correct |
3 ms |
7360 KB |
Output is correct |
4 |
Correct |
3 ms |
7288 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7304 KB |
Output is correct |
8 |
Correct |
3 ms |
7364 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7360 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7364 KB |
Output is correct |
14 |
Correct |
3 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7380 KB |
Output is correct |
18 |
Correct |
8 ms |
8396 KB |
Output is correct |
19 |
Correct |
8 ms |
8360 KB |
Output is correct |
20 |
Correct |
8 ms |
8404 KB |
Output is correct |
21 |
Correct |
8 ms |
8404 KB |
Output is correct |
22 |
Correct |
8 ms |
8404 KB |
Output is correct |
23 |
Correct |
11 ms |
8396 KB |
Output is correct |
24 |
Correct |
7 ms |
8392 KB |
Output is correct |
25 |
Correct |
5 ms |
7360 KB |
Output is correct |
26 |
Correct |
11 ms |
8268 KB |
Output is correct |
27 |
Correct |
8 ms |
8276 KB |
Output is correct |
28 |
Correct |
7 ms |
8232 KB |
Output is correct |
29 |
Correct |
8 ms |
8276 KB |
Output is correct |
30 |
Correct |
8 ms |
8144 KB |
Output is correct |
31 |
Correct |
8 ms |
8140 KB |
Output is correct |
32 |
Correct |
8 ms |
8104 KB |
Output is correct |
33 |
Correct |
8 ms |
8132 KB |
Output is correct |
34 |
Correct |
204 ms |
49452 KB |
Output is correct |
35 |
Correct |
219 ms |
49400 KB |
Output is correct |
36 |
Correct |
211 ms |
49460 KB |
Output is correct |
37 |
Correct |
269 ms |
49468 KB |
Output is correct |
38 |
Correct |
264 ms |
49428 KB |
Output is correct |
39 |
Correct |
293 ms |
49388 KB |
Output is correct |
40 |
Correct |
295 ms |
49476 KB |
Output is correct |
41 |
Correct |
4 ms |
7360 KB |
Output is correct |
42 |
Correct |
4 ms |
7360 KB |
Output is correct |
43 |
Correct |
257 ms |
39988 KB |
Output is correct |
44 |
Correct |
298 ms |
39984 KB |
Output is correct |
45 |
Correct |
260 ms |
39980 KB |
Output is correct |
46 |
Correct |
376 ms |
45220 KB |
Output is correct |
47 |
Correct |
369 ms |
44444 KB |
Output is correct |
48 |
Correct |
355 ms |
45660 KB |
Output is correct |
49 |
Correct |
363 ms |
44408 KB |
Output is correct |
50 |
Correct |
328 ms |
40212 KB |
Output is correct |
51 |
Correct |
337 ms |
40264 KB |
Output is correct |
52 |
Correct |
350 ms |
40172 KB |
Output is correct |
53 |
Correct |
346 ms |
40268 KB |
Output is correct |