#include <bits/stdc++.h>
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));
}
int 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 |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9652 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9748 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9652 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9748 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9816 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9960 KB |
Output is correct |
14 |
Correct |
6 ms |
9756 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 |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9652 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9748 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9816 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9960 KB |
Output is correct |
14 |
Correct |
6 ms |
9756 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 |
11096 KB |
Output is correct |
19 |
Correct |
6 ms |
10892 KB |
Output is correct |
20 |
Correct |
6 ms |
11160 KB |
Output is correct |
21 |
Correct |
6 ms |
10844 KB |
Output is correct |
22 |
Correct |
6 ms |
10896 KB |
Output is correct |
23 |
Correct |
6 ms |
10844 KB |
Output is correct |
24 |
Correct |
6 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9652 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9748 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9816 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9960 KB |
Output is correct |
14 |
Correct |
6 ms |
9756 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 |
11096 KB |
Output is correct |
19 |
Correct |
6 ms |
10892 KB |
Output is correct |
20 |
Correct |
6 ms |
11160 KB |
Output is correct |
21 |
Correct |
6 ms |
10844 KB |
Output is correct |
22 |
Correct |
6 ms |
10896 KB |
Output is correct |
23 |
Correct |
6 ms |
10844 KB |
Output is correct |
24 |
Correct |
6 ms |
10844 KB |
Output is correct |
25 |
Correct |
4 ms |
9816 KB |
Output is correct |
26 |
Correct |
7 ms |
10844 KB |
Output is correct |
27 |
Correct |
7 ms |
11092 KB |
Output is correct |
28 |
Correct |
7 ms |
10844 KB |
Output is correct |
29 |
Correct |
8 ms |
10844 KB |
Output is correct |
30 |
Correct |
7 ms |
10600 KB |
Output is correct |
31 |
Correct |
7 ms |
10688 KB |
Output is correct |
32 |
Correct |
6 ms |
10532 KB |
Output is correct |
33 |
Correct |
7 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9652 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9748 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9816 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9960 KB |
Output is correct |
14 |
Correct |
6 ms |
9756 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 |
11096 KB |
Output is correct |
19 |
Correct |
6 ms |
10892 KB |
Output is correct |
20 |
Correct |
6 ms |
11160 KB |
Output is correct |
21 |
Correct |
6 ms |
10844 KB |
Output is correct |
22 |
Correct |
6 ms |
10896 KB |
Output is correct |
23 |
Correct |
6 ms |
10844 KB |
Output is correct |
24 |
Correct |
6 ms |
10844 KB |
Output is correct |
25 |
Incorrect |
106 ms |
61340 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
134 ms |
44372 KB |
Output is correct |
4 |
Correct |
138 ms |
44540 KB |
Output is correct |
5 |
Correct |
126 ms |
44300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9652 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
5 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9748 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
5 ms |
9816 KB |
Output is correct |
12 |
Correct |
4 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9960 KB |
Output is correct |
14 |
Correct |
6 ms |
9756 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 |
11096 KB |
Output is correct |
19 |
Correct |
6 ms |
10892 KB |
Output is correct |
20 |
Correct |
6 ms |
11160 KB |
Output is correct |
21 |
Correct |
6 ms |
10844 KB |
Output is correct |
22 |
Correct |
6 ms |
10896 KB |
Output is correct |
23 |
Correct |
6 ms |
10844 KB |
Output is correct |
24 |
Correct |
6 ms |
10844 KB |
Output is correct |
25 |
Correct |
4 ms |
9816 KB |
Output is correct |
26 |
Correct |
7 ms |
10844 KB |
Output is correct |
27 |
Correct |
7 ms |
11092 KB |
Output is correct |
28 |
Correct |
7 ms |
10844 KB |
Output is correct |
29 |
Correct |
8 ms |
10844 KB |
Output is correct |
30 |
Correct |
7 ms |
10600 KB |
Output is correct |
31 |
Correct |
7 ms |
10688 KB |
Output is correct |
32 |
Correct |
6 ms |
10532 KB |
Output is correct |
33 |
Correct |
7 ms |
10588 KB |
Output is correct |
34 |
Incorrect |
106 ms |
61340 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |