#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,h[200001],p[200001][20],d[200001],pos[200001],l[200001],u,v,mx[200001],res;
vector <int> ke[200001],ke2[200001];
void dfs(int u, int par){
for (int v:ke[u])
if (v!=par){
p[v][0]=u;
for (int i=1;i<20;i++)
p[v][i]=p[p[v][i-1]][i-1];
d[v]=d[u]+1;
dfs(v,u);
}
}
int lca(int u, int v){
if (d[u]<d[v])
swap(u,v);
for (int i=19;i>=0;i--)
if (d[p[u][i]]>=d[v])
u=p[u][i];
if (u==v)
return u;
for (int i=19;i>=0;i--)
if (p[u][i]!=p[v][i]){
u=p[u][i];
v=p[v][i];
}
return p[u][0];
}
int dist(int u, int v){
return d[u]+d[v]-d[lca(u,v)]*2;
}
int f(int i){
return (l[i]==i?i:l[i]=f(l[i]));
}
void unite(int i, int j){
i=f(i),j=f(j);
l[i]=j;
mx[j]=max(mx[i],mx[j]);
}
void dfs2(int u, int val){
res=max(res,val);
for (int v:ke2[u])
dfs2(v,val+dist(u,v));
}
int32_t main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n;
for (int i=1;i<=n;i++){
cin >> h[i];
pos[h[i]]=i;
l[i]=i;
mx[i]=h[i];
}
for (int i=1;i<n;i++){
cin >> u >> v;
ke[u].push_back(v);
ke[v].push_back(u);
}
dfs(pos[n],0);
for (int i=1;i<=n;i++){
int u=pos[i];
for (int j:ke[u])
if (h[j]<h[u]){
int v=pos[mx[f(j)]];
ke2[u].push_back(v);
unite(u,j);
}
}
dfs2(pos[n],0);
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9892 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9892 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9660 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9988 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9892 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9660 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9988 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
6 ms |
11356 KB |
Output is correct |
20 |
Correct |
8 ms |
11356 KB |
Output is correct |
21 |
Correct |
6 ms |
11352 KB |
Output is correct |
22 |
Correct |
6 ms |
11356 KB |
Output is correct |
23 |
Correct |
6 ms |
11372 KB |
Output is correct |
24 |
Correct |
6 ms |
11156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9892 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9660 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9988 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
6 ms |
11356 KB |
Output is correct |
20 |
Correct |
8 ms |
11356 KB |
Output is correct |
21 |
Correct |
6 ms |
11352 KB |
Output is correct |
22 |
Correct |
6 ms |
11356 KB |
Output is correct |
23 |
Correct |
6 ms |
11372 KB |
Output is correct |
24 |
Correct |
6 ms |
11156 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
9 ms |
11356 KB |
Output is correct |
27 |
Correct |
7 ms |
11356 KB |
Output is correct |
28 |
Correct |
8 ms |
11356 KB |
Output is correct |
29 |
Correct |
9 ms |
11356 KB |
Output is correct |
30 |
Correct |
7 ms |
11052 KB |
Output is correct |
31 |
Correct |
7 ms |
11096 KB |
Output is correct |
32 |
Correct |
7 ms |
11100 KB |
Output is correct |
33 |
Correct |
7 ms |
11100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9892 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9660 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9988 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
6 ms |
11356 KB |
Output is correct |
20 |
Correct |
8 ms |
11356 KB |
Output is correct |
21 |
Correct |
6 ms |
11352 KB |
Output is correct |
22 |
Correct |
6 ms |
11356 KB |
Output is correct |
23 |
Correct |
6 ms |
11372 KB |
Output is correct |
24 |
Correct |
6 ms |
11156 KB |
Output is correct |
25 |
Correct |
118 ms |
77652 KB |
Output is correct |
26 |
Correct |
134 ms |
77140 KB |
Output is correct |
27 |
Correct |
134 ms |
77140 KB |
Output is correct |
28 |
Correct |
141 ms |
72532 KB |
Output is correct |
29 |
Correct |
144 ms |
74064 KB |
Output is correct |
30 |
Correct |
150 ms |
73812 KB |
Output is correct |
31 |
Correct |
152 ms |
74320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9864 KB |
Output is correct |
3 |
Correct |
148 ms |
65424 KB |
Output is correct |
4 |
Correct |
153 ms |
65364 KB |
Output is correct |
5 |
Correct |
157 ms |
65360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
5 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9892 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9660 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9988 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
6 ms |
11356 KB |
Output is correct |
19 |
Correct |
6 ms |
11356 KB |
Output is correct |
20 |
Correct |
8 ms |
11356 KB |
Output is correct |
21 |
Correct |
6 ms |
11352 KB |
Output is correct |
22 |
Correct |
6 ms |
11356 KB |
Output is correct |
23 |
Correct |
6 ms |
11372 KB |
Output is correct |
24 |
Correct |
6 ms |
11156 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
9 ms |
11356 KB |
Output is correct |
27 |
Correct |
7 ms |
11356 KB |
Output is correct |
28 |
Correct |
8 ms |
11356 KB |
Output is correct |
29 |
Correct |
9 ms |
11356 KB |
Output is correct |
30 |
Correct |
7 ms |
11052 KB |
Output is correct |
31 |
Correct |
7 ms |
11096 KB |
Output is correct |
32 |
Correct |
7 ms |
11100 KB |
Output is correct |
33 |
Correct |
7 ms |
11100 KB |
Output is correct |
34 |
Correct |
118 ms |
77652 KB |
Output is correct |
35 |
Correct |
134 ms |
77140 KB |
Output is correct |
36 |
Correct |
134 ms |
77140 KB |
Output is correct |
37 |
Correct |
141 ms |
72532 KB |
Output is correct |
38 |
Correct |
144 ms |
74064 KB |
Output is correct |
39 |
Correct |
150 ms |
73812 KB |
Output is correct |
40 |
Correct |
152 ms |
74320 KB |
Output is correct |
41 |
Correct |
4 ms |
9820 KB |
Output is correct |
42 |
Correct |
4 ms |
9864 KB |
Output is correct |
43 |
Correct |
148 ms |
65424 KB |
Output is correct |
44 |
Correct |
153 ms |
65364 KB |
Output is correct |
45 |
Correct |
157 ms |
65360 KB |
Output is correct |
46 |
Correct |
307 ms |
73580 KB |
Output is correct |
47 |
Correct |
266 ms |
73576 KB |
Output is correct |
48 |
Correct |
279 ms |
74840 KB |
Output is correct |
49 |
Correct |
301 ms |
73552 KB |
Output is correct |
50 |
Correct |
227 ms |
64344 KB |
Output is correct |
51 |
Correct |
211 ms |
64336 KB |
Output is correct |
52 |
Correct |
220 ms |
64336 KB |
Output is correct |
53 |
Correct |
245 ms |
64452 KB |
Output is correct |