#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define all(x) x.begin(), x.end()
const int N=200005;
vector<int> pa(N);
int Find(int k){
return (pa[k]==k?k:pa[k]=Find(pa[k]));
}
void onion(int a,int b){
a=Find(a),b=Find(b);
if(a!=b)pa[a]=b;
}
int32_t main() {
LCBorz;
iota(all(pa),0);
int n;cin>>n;
vector<int> v(N),v1(N),dp(N),adj[N],dis(N);
vector jump(18,vector(N,0));
for(int i=1;i<=n;i++){
cin>>v[i];
v1[v[i]]=i;
}
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
auto dfs=[&](auto dfs1,int k)->void {
for(int i=1;i<18;i++){
jump[i][k]=jump[i-1][jump[i-1][k]];
}
for(int j:adj[k]){
if(j==jump[0][k])continue;
jump[0][j]=k;
dis[j]=dis[k]+1;
dfs1(dfs1,j);
}
};
auto lca=[&](int a,int b)->int {
if(dis[a]<dis[b])swap(a,b);
int d=dis[a]-dis[b];
for(int i=17;i>=0;i--){
if(d&(1<<i)){
a=jump[i][a];
}
}
if(a==b)return a;
for(int i=17;i>=0;i--){
if(jump[i][a]!=jump[i][b]){
a=jump[i][a],b=jump[i][b];
}
}
return jump[0][a];
};
auto far=[&](int a,int b){
return dis[a]+dis[b]-2*dis[lca(a,b)];
};
jump[0][1]=1;
dfs(dfs,1);
for(int i1=1;i1<=n;i1++){
int i=v1[i1];
for(int j:adj[i]){
if(v[j]>v[i])continue;
dp[i]=max(dp[i],dp[Find(j)]+far(i,Find(j)));
onion(j,i);
}
}
cout<<dp[v1[n]]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
14 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23896 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23788 KB |
Output is correct |
6 |
Correct |
11 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
14 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23896 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23788 KB |
Output is correct |
6 |
Correct |
11 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
11 ms |
23896 KB |
Output is correct |
12 |
Correct |
11 ms |
23896 KB |
Output is correct |
13 |
Correct |
11 ms |
23900 KB |
Output is correct |
14 |
Correct |
11 ms |
23900 KB |
Output is correct |
15 |
Correct |
11 ms |
23820 KB |
Output is correct |
16 |
Correct |
11 ms |
23896 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
14 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23896 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23788 KB |
Output is correct |
6 |
Correct |
11 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
11 ms |
23896 KB |
Output is correct |
12 |
Correct |
11 ms |
23896 KB |
Output is correct |
13 |
Correct |
11 ms |
23900 KB |
Output is correct |
14 |
Correct |
11 ms |
23900 KB |
Output is correct |
15 |
Correct |
11 ms |
23820 KB |
Output is correct |
16 |
Correct |
11 ms |
23896 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
14 ms |
24020 KB |
Output is correct |
19 |
Correct |
14 ms |
23900 KB |
Output is correct |
20 |
Correct |
14 ms |
23900 KB |
Output is correct |
21 |
Correct |
14 ms |
23900 KB |
Output is correct |
22 |
Correct |
14 ms |
23896 KB |
Output is correct |
23 |
Correct |
14 ms |
23900 KB |
Output is correct |
24 |
Correct |
14 ms |
24012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
14 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23896 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23788 KB |
Output is correct |
6 |
Correct |
11 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
11 ms |
23896 KB |
Output is correct |
12 |
Correct |
11 ms |
23896 KB |
Output is correct |
13 |
Correct |
11 ms |
23900 KB |
Output is correct |
14 |
Correct |
11 ms |
23900 KB |
Output is correct |
15 |
Correct |
11 ms |
23820 KB |
Output is correct |
16 |
Correct |
11 ms |
23896 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
14 ms |
24020 KB |
Output is correct |
19 |
Correct |
14 ms |
23900 KB |
Output is correct |
20 |
Correct |
14 ms |
23900 KB |
Output is correct |
21 |
Correct |
14 ms |
23900 KB |
Output is correct |
22 |
Correct |
14 ms |
23896 KB |
Output is correct |
23 |
Correct |
14 ms |
23900 KB |
Output is correct |
24 |
Correct |
14 ms |
24012 KB |
Output is correct |
25 |
Correct |
11 ms |
23896 KB |
Output is correct |
26 |
Correct |
15 ms |
24308 KB |
Output is correct |
27 |
Correct |
14 ms |
23904 KB |
Output is correct |
28 |
Correct |
14 ms |
23900 KB |
Output is correct |
29 |
Correct |
14 ms |
23840 KB |
Output is correct |
30 |
Correct |
14 ms |
23900 KB |
Output is correct |
31 |
Correct |
14 ms |
23900 KB |
Output is correct |
32 |
Correct |
14 ms |
23900 KB |
Output is correct |
33 |
Correct |
14 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
14 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23896 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23788 KB |
Output is correct |
6 |
Correct |
11 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
11 ms |
23896 KB |
Output is correct |
12 |
Correct |
11 ms |
23896 KB |
Output is correct |
13 |
Correct |
11 ms |
23900 KB |
Output is correct |
14 |
Correct |
11 ms |
23900 KB |
Output is correct |
15 |
Correct |
11 ms |
23820 KB |
Output is correct |
16 |
Correct |
11 ms |
23896 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
14 ms |
24020 KB |
Output is correct |
19 |
Correct |
14 ms |
23900 KB |
Output is correct |
20 |
Correct |
14 ms |
23900 KB |
Output is correct |
21 |
Correct |
14 ms |
23900 KB |
Output is correct |
22 |
Correct |
14 ms |
23896 KB |
Output is correct |
23 |
Correct |
14 ms |
23900 KB |
Output is correct |
24 |
Correct |
14 ms |
24012 KB |
Output is correct |
25 |
Incorrect |
133 ms |
41464 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23896 KB |
Output is correct |
2 |
Correct |
11 ms |
23900 KB |
Output is correct |
3 |
Correct |
174 ms |
33136 KB |
Output is correct |
4 |
Correct |
185 ms |
33064 KB |
Output is correct |
5 |
Correct |
174 ms |
33088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23896 KB |
Output is correct |
2 |
Correct |
14 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23896 KB |
Output is correct |
4 |
Correct |
12 ms |
23900 KB |
Output is correct |
5 |
Correct |
11 ms |
23788 KB |
Output is correct |
6 |
Correct |
11 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
23884 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
11 ms |
23900 KB |
Output is correct |
11 |
Correct |
11 ms |
23896 KB |
Output is correct |
12 |
Correct |
11 ms |
23896 KB |
Output is correct |
13 |
Correct |
11 ms |
23900 KB |
Output is correct |
14 |
Correct |
11 ms |
23900 KB |
Output is correct |
15 |
Correct |
11 ms |
23820 KB |
Output is correct |
16 |
Correct |
11 ms |
23896 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
14 ms |
24020 KB |
Output is correct |
19 |
Correct |
14 ms |
23900 KB |
Output is correct |
20 |
Correct |
14 ms |
23900 KB |
Output is correct |
21 |
Correct |
14 ms |
23900 KB |
Output is correct |
22 |
Correct |
14 ms |
23896 KB |
Output is correct |
23 |
Correct |
14 ms |
23900 KB |
Output is correct |
24 |
Correct |
14 ms |
24012 KB |
Output is correct |
25 |
Correct |
11 ms |
23896 KB |
Output is correct |
26 |
Correct |
15 ms |
24308 KB |
Output is correct |
27 |
Correct |
14 ms |
23904 KB |
Output is correct |
28 |
Correct |
14 ms |
23900 KB |
Output is correct |
29 |
Correct |
14 ms |
23840 KB |
Output is correct |
30 |
Correct |
14 ms |
23900 KB |
Output is correct |
31 |
Correct |
14 ms |
23900 KB |
Output is correct |
32 |
Correct |
14 ms |
23900 KB |
Output is correct |
33 |
Correct |
14 ms |
23900 KB |
Output is correct |
34 |
Incorrect |
133 ms |
41464 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |