#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;
const int K=20;
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(K,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<K;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=K-1;i>=0;i--){
if(d&(1<<i)){
a=jump[i][a];
}
}
if(a==b)return a;
for(int i=K-1;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 |
13 ms |
25436 KB |
Output is correct |
2 |
Correct |
12 ms |
25396 KB |
Output is correct |
3 |
Correct |
11 ms |
25436 KB |
Output is correct |
4 |
Correct |
12 ms |
25436 KB |
Output is correct |
5 |
Correct |
11 ms |
25572 KB |
Output is correct |
6 |
Correct |
12 ms |
25452 KB |
Output is correct |
7 |
Correct |
11 ms |
25436 KB |
Output is correct |
8 |
Correct |
12 ms |
25436 KB |
Output is correct |
9 |
Correct |
11 ms |
25432 KB |
Output is correct |
10 |
Correct |
11 ms |
25436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
25436 KB |
Output is correct |
2 |
Correct |
12 ms |
25396 KB |
Output is correct |
3 |
Correct |
11 ms |
25436 KB |
Output is correct |
4 |
Correct |
12 ms |
25436 KB |
Output is correct |
5 |
Correct |
11 ms |
25572 KB |
Output is correct |
6 |
Correct |
12 ms |
25452 KB |
Output is correct |
7 |
Correct |
11 ms |
25436 KB |
Output is correct |
8 |
Correct |
12 ms |
25436 KB |
Output is correct |
9 |
Correct |
11 ms |
25432 KB |
Output is correct |
10 |
Correct |
11 ms |
25436 KB |
Output is correct |
11 |
Correct |
14 ms |
25436 KB |
Output is correct |
12 |
Correct |
12 ms |
25436 KB |
Output is correct |
13 |
Correct |
12 ms |
25432 KB |
Output is correct |
14 |
Correct |
13 ms |
25400 KB |
Output is correct |
15 |
Correct |
13 ms |
25436 KB |
Output is correct |
16 |
Correct |
12 ms |
25436 KB |
Output is correct |
17 |
Correct |
12 ms |
25432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
25436 KB |
Output is correct |
2 |
Correct |
12 ms |
25396 KB |
Output is correct |
3 |
Correct |
11 ms |
25436 KB |
Output is correct |
4 |
Correct |
12 ms |
25436 KB |
Output is correct |
5 |
Correct |
11 ms |
25572 KB |
Output is correct |
6 |
Correct |
12 ms |
25452 KB |
Output is correct |
7 |
Correct |
11 ms |
25436 KB |
Output is correct |
8 |
Correct |
12 ms |
25436 KB |
Output is correct |
9 |
Correct |
11 ms |
25432 KB |
Output is correct |
10 |
Correct |
11 ms |
25436 KB |
Output is correct |
11 |
Correct |
14 ms |
25436 KB |
Output is correct |
12 |
Correct |
12 ms |
25436 KB |
Output is correct |
13 |
Correct |
12 ms |
25432 KB |
Output is correct |
14 |
Correct |
13 ms |
25400 KB |
Output is correct |
15 |
Correct |
13 ms |
25436 KB |
Output is correct |
16 |
Correct |
12 ms |
25436 KB |
Output is correct |
17 |
Correct |
12 ms |
25432 KB |
Output is correct |
18 |
Correct |
15 ms |
25436 KB |
Output is correct |
19 |
Correct |
15 ms |
25436 KB |
Output is correct |
20 |
Correct |
15 ms |
25684 KB |
Output is correct |
21 |
Correct |
16 ms |
25432 KB |
Output is correct |
22 |
Correct |
15 ms |
25436 KB |
Output is correct |
23 |
Correct |
15 ms |
25516 KB |
Output is correct |
24 |
Correct |
15 ms |
25436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
25436 KB |
Output is correct |
2 |
Correct |
12 ms |
25396 KB |
Output is correct |
3 |
Correct |
11 ms |
25436 KB |
Output is correct |
4 |
Correct |
12 ms |
25436 KB |
Output is correct |
5 |
Correct |
11 ms |
25572 KB |
Output is correct |
6 |
Correct |
12 ms |
25452 KB |
Output is correct |
7 |
Correct |
11 ms |
25436 KB |
Output is correct |
8 |
Correct |
12 ms |
25436 KB |
Output is correct |
9 |
Correct |
11 ms |
25432 KB |
Output is correct |
10 |
Correct |
11 ms |
25436 KB |
Output is correct |
11 |
Correct |
14 ms |
25436 KB |
Output is correct |
12 |
Correct |
12 ms |
25436 KB |
Output is correct |
13 |
Correct |
12 ms |
25432 KB |
Output is correct |
14 |
Correct |
13 ms |
25400 KB |
Output is correct |
15 |
Correct |
13 ms |
25436 KB |
Output is correct |
16 |
Correct |
12 ms |
25436 KB |
Output is correct |
17 |
Correct |
12 ms |
25432 KB |
Output is correct |
18 |
Correct |
15 ms |
25436 KB |
Output is correct |
19 |
Correct |
15 ms |
25436 KB |
Output is correct |
20 |
Correct |
15 ms |
25684 KB |
Output is correct |
21 |
Correct |
16 ms |
25432 KB |
Output is correct |
22 |
Correct |
15 ms |
25436 KB |
Output is correct |
23 |
Correct |
15 ms |
25516 KB |
Output is correct |
24 |
Correct |
15 ms |
25436 KB |
Output is correct |
25 |
Correct |
12 ms |
25436 KB |
Output is correct |
26 |
Correct |
15 ms |
25436 KB |
Output is correct |
27 |
Correct |
15 ms |
25436 KB |
Output is correct |
28 |
Correct |
15 ms |
25436 KB |
Output is correct |
29 |
Correct |
16 ms |
25456 KB |
Output is correct |
30 |
Correct |
15 ms |
25436 KB |
Output is correct |
31 |
Correct |
15 ms |
25552 KB |
Output is correct |
32 |
Correct |
15 ms |
25556 KB |
Output is correct |
33 |
Correct |
15 ms |
25396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
25436 KB |
Output is correct |
2 |
Correct |
12 ms |
25396 KB |
Output is correct |
3 |
Correct |
11 ms |
25436 KB |
Output is correct |
4 |
Correct |
12 ms |
25436 KB |
Output is correct |
5 |
Correct |
11 ms |
25572 KB |
Output is correct |
6 |
Correct |
12 ms |
25452 KB |
Output is correct |
7 |
Correct |
11 ms |
25436 KB |
Output is correct |
8 |
Correct |
12 ms |
25436 KB |
Output is correct |
9 |
Correct |
11 ms |
25432 KB |
Output is correct |
10 |
Correct |
11 ms |
25436 KB |
Output is correct |
11 |
Correct |
14 ms |
25436 KB |
Output is correct |
12 |
Correct |
12 ms |
25436 KB |
Output is correct |
13 |
Correct |
12 ms |
25432 KB |
Output is correct |
14 |
Correct |
13 ms |
25400 KB |
Output is correct |
15 |
Correct |
13 ms |
25436 KB |
Output is correct |
16 |
Correct |
12 ms |
25436 KB |
Output is correct |
17 |
Correct |
12 ms |
25432 KB |
Output is correct |
18 |
Correct |
15 ms |
25436 KB |
Output is correct |
19 |
Correct |
15 ms |
25436 KB |
Output is correct |
20 |
Correct |
15 ms |
25684 KB |
Output is correct |
21 |
Correct |
16 ms |
25432 KB |
Output is correct |
22 |
Correct |
15 ms |
25436 KB |
Output is correct |
23 |
Correct |
15 ms |
25516 KB |
Output is correct |
24 |
Correct |
15 ms |
25436 KB |
Output is correct |
25 |
Incorrect |
143 ms |
39352 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
25432 KB |
Output is correct |
2 |
Correct |
12 ms |
25436 KB |
Output is correct |
3 |
Correct |
187 ms |
31020 KB |
Output is correct |
4 |
Correct |
184 ms |
31040 KB |
Output is correct |
5 |
Correct |
187 ms |
31040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
25436 KB |
Output is correct |
2 |
Correct |
12 ms |
25396 KB |
Output is correct |
3 |
Correct |
11 ms |
25436 KB |
Output is correct |
4 |
Correct |
12 ms |
25436 KB |
Output is correct |
5 |
Correct |
11 ms |
25572 KB |
Output is correct |
6 |
Correct |
12 ms |
25452 KB |
Output is correct |
7 |
Correct |
11 ms |
25436 KB |
Output is correct |
8 |
Correct |
12 ms |
25436 KB |
Output is correct |
9 |
Correct |
11 ms |
25432 KB |
Output is correct |
10 |
Correct |
11 ms |
25436 KB |
Output is correct |
11 |
Correct |
14 ms |
25436 KB |
Output is correct |
12 |
Correct |
12 ms |
25436 KB |
Output is correct |
13 |
Correct |
12 ms |
25432 KB |
Output is correct |
14 |
Correct |
13 ms |
25400 KB |
Output is correct |
15 |
Correct |
13 ms |
25436 KB |
Output is correct |
16 |
Correct |
12 ms |
25436 KB |
Output is correct |
17 |
Correct |
12 ms |
25432 KB |
Output is correct |
18 |
Correct |
15 ms |
25436 KB |
Output is correct |
19 |
Correct |
15 ms |
25436 KB |
Output is correct |
20 |
Correct |
15 ms |
25684 KB |
Output is correct |
21 |
Correct |
16 ms |
25432 KB |
Output is correct |
22 |
Correct |
15 ms |
25436 KB |
Output is correct |
23 |
Correct |
15 ms |
25516 KB |
Output is correct |
24 |
Correct |
15 ms |
25436 KB |
Output is correct |
25 |
Correct |
12 ms |
25436 KB |
Output is correct |
26 |
Correct |
15 ms |
25436 KB |
Output is correct |
27 |
Correct |
15 ms |
25436 KB |
Output is correct |
28 |
Correct |
15 ms |
25436 KB |
Output is correct |
29 |
Correct |
16 ms |
25456 KB |
Output is correct |
30 |
Correct |
15 ms |
25436 KB |
Output is correct |
31 |
Correct |
15 ms |
25552 KB |
Output is correct |
32 |
Correct |
15 ms |
25556 KB |
Output is correct |
33 |
Correct |
15 ms |
25396 KB |
Output is correct |
34 |
Incorrect |
143 ms |
39352 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |