#include <bits/stdc++.h>
using namespace std;
#define int long long
#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;
int p=Find(j);
dp[i]=max(dp[i],dp[p]+far(i,p));
onion(p,i);
}
}
cout<<dp[v1[n]]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
29272 KB |
Output is correct |
2 |
Correct |
13 ms |
29528 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
13 ms |
29276 KB |
Output is correct |
5 |
Correct |
16 ms |
29280 KB |
Output is correct |
6 |
Correct |
13 ms |
29276 KB |
Output is correct |
7 |
Correct |
13 ms |
29272 KB |
Output is correct |
8 |
Correct |
13 ms |
29276 KB |
Output is correct |
9 |
Correct |
13 ms |
29312 KB |
Output is correct |
10 |
Correct |
13 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
29272 KB |
Output is correct |
2 |
Correct |
13 ms |
29528 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
13 ms |
29276 KB |
Output is correct |
5 |
Correct |
16 ms |
29280 KB |
Output is correct |
6 |
Correct |
13 ms |
29276 KB |
Output is correct |
7 |
Correct |
13 ms |
29272 KB |
Output is correct |
8 |
Correct |
13 ms |
29276 KB |
Output is correct |
9 |
Correct |
13 ms |
29312 KB |
Output is correct |
10 |
Correct |
13 ms |
29276 KB |
Output is correct |
11 |
Correct |
14 ms |
29272 KB |
Output is correct |
12 |
Correct |
13 ms |
29276 KB |
Output is correct |
13 |
Correct |
14 ms |
29276 KB |
Output is correct |
14 |
Correct |
13 ms |
29276 KB |
Output is correct |
15 |
Correct |
13 ms |
29276 KB |
Output is correct |
16 |
Correct |
13 ms |
29276 KB |
Output is correct |
17 |
Correct |
13 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
29272 KB |
Output is correct |
2 |
Correct |
13 ms |
29528 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
13 ms |
29276 KB |
Output is correct |
5 |
Correct |
16 ms |
29280 KB |
Output is correct |
6 |
Correct |
13 ms |
29276 KB |
Output is correct |
7 |
Correct |
13 ms |
29272 KB |
Output is correct |
8 |
Correct |
13 ms |
29276 KB |
Output is correct |
9 |
Correct |
13 ms |
29312 KB |
Output is correct |
10 |
Correct |
13 ms |
29276 KB |
Output is correct |
11 |
Correct |
14 ms |
29272 KB |
Output is correct |
12 |
Correct |
13 ms |
29276 KB |
Output is correct |
13 |
Correct |
14 ms |
29276 KB |
Output is correct |
14 |
Correct |
13 ms |
29276 KB |
Output is correct |
15 |
Correct |
13 ms |
29276 KB |
Output is correct |
16 |
Correct |
13 ms |
29276 KB |
Output is correct |
17 |
Correct |
13 ms |
29276 KB |
Output is correct |
18 |
Correct |
15 ms |
29276 KB |
Output is correct |
19 |
Correct |
16 ms |
29276 KB |
Output is correct |
20 |
Correct |
15 ms |
29276 KB |
Output is correct |
21 |
Correct |
16 ms |
29276 KB |
Output is correct |
22 |
Correct |
15 ms |
29276 KB |
Output is correct |
23 |
Correct |
15 ms |
29276 KB |
Output is correct |
24 |
Correct |
15 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
29272 KB |
Output is correct |
2 |
Correct |
13 ms |
29528 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
13 ms |
29276 KB |
Output is correct |
5 |
Correct |
16 ms |
29280 KB |
Output is correct |
6 |
Correct |
13 ms |
29276 KB |
Output is correct |
7 |
Correct |
13 ms |
29272 KB |
Output is correct |
8 |
Correct |
13 ms |
29276 KB |
Output is correct |
9 |
Correct |
13 ms |
29312 KB |
Output is correct |
10 |
Correct |
13 ms |
29276 KB |
Output is correct |
11 |
Correct |
14 ms |
29272 KB |
Output is correct |
12 |
Correct |
13 ms |
29276 KB |
Output is correct |
13 |
Correct |
14 ms |
29276 KB |
Output is correct |
14 |
Correct |
13 ms |
29276 KB |
Output is correct |
15 |
Correct |
13 ms |
29276 KB |
Output is correct |
16 |
Correct |
13 ms |
29276 KB |
Output is correct |
17 |
Correct |
13 ms |
29276 KB |
Output is correct |
18 |
Correct |
15 ms |
29276 KB |
Output is correct |
19 |
Correct |
16 ms |
29276 KB |
Output is correct |
20 |
Correct |
15 ms |
29276 KB |
Output is correct |
21 |
Correct |
16 ms |
29276 KB |
Output is correct |
22 |
Correct |
15 ms |
29276 KB |
Output is correct |
23 |
Correct |
15 ms |
29276 KB |
Output is correct |
24 |
Correct |
15 ms |
29276 KB |
Output is correct |
25 |
Correct |
13 ms |
29272 KB |
Output is correct |
26 |
Correct |
17 ms |
29276 KB |
Output is correct |
27 |
Correct |
17 ms |
29276 KB |
Output is correct |
28 |
Correct |
16 ms |
29276 KB |
Output is correct |
29 |
Correct |
16 ms |
29344 KB |
Output is correct |
30 |
Correct |
16 ms |
29276 KB |
Output is correct |
31 |
Correct |
17 ms |
29488 KB |
Output is correct |
32 |
Correct |
16 ms |
29272 KB |
Output is correct |
33 |
Correct |
16 ms |
29528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
29272 KB |
Output is correct |
2 |
Correct |
13 ms |
29528 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
13 ms |
29276 KB |
Output is correct |
5 |
Correct |
16 ms |
29280 KB |
Output is correct |
6 |
Correct |
13 ms |
29276 KB |
Output is correct |
7 |
Correct |
13 ms |
29272 KB |
Output is correct |
8 |
Correct |
13 ms |
29276 KB |
Output is correct |
9 |
Correct |
13 ms |
29312 KB |
Output is correct |
10 |
Correct |
13 ms |
29276 KB |
Output is correct |
11 |
Correct |
14 ms |
29272 KB |
Output is correct |
12 |
Correct |
13 ms |
29276 KB |
Output is correct |
13 |
Correct |
14 ms |
29276 KB |
Output is correct |
14 |
Correct |
13 ms |
29276 KB |
Output is correct |
15 |
Correct |
13 ms |
29276 KB |
Output is correct |
16 |
Correct |
13 ms |
29276 KB |
Output is correct |
17 |
Correct |
13 ms |
29276 KB |
Output is correct |
18 |
Correct |
15 ms |
29276 KB |
Output is correct |
19 |
Correct |
16 ms |
29276 KB |
Output is correct |
20 |
Correct |
15 ms |
29276 KB |
Output is correct |
21 |
Correct |
16 ms |
29276 KB |
Output is correct |
22 |
Correct |
15 ms |
29276 KB |
Output is correct |
23 |
Correct |
15 ms |
29276 KB |
Output is correct |
24 |
Correct |
15 ms |
29276 KB |
Output is correct |
25 |
Correct |
113 ms |
45496 KB |
Output is correct |
26 |
Correct |
119 ms |
49472 KB |
Output is correct |
27 |
Correct |
123 ms |
49516 KB |
Output is correct |
28 |
Correct |
172 ms |
49620 KB |
Output is correct |
29 |
Correct |
153 ms |
49464 KB |
Output is correct |
30 |
Correct |
148 ms |
49512 KB |
Output is correct |
31 |
Correct |
154 ms |
49508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
29460 KB |
Output is correct |
2 |
Correct |
13 ms |
29272 KB |
Output is correct |
3 |
Correct |
161 ms |
36352 KB |
Output is correct |
4 |
Correct |
162 ms |
36416 KB |
Output is correct |
5 |
Correct |
204 ms |
36416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
29272 KB |
Output is correct |
2 |
Correct |
13 ms |
29528 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
13 ms |
29276 KB |
Output is correct |
5 |
Correct |
16 ms |
29280 KB |
Output is correct |
6 |
Correct |
13 ms |
29276 KB |
Output is correct |
7 |
Correct |
13 ms |
29272 KB |
Output is correct |
8 |
Correct |
13 ms |
29276 KB |
Output is correct |
9 |
Correct |
13 ms |
29312 KB |
Output is correct |
10 |
Correct |
13 ms |
29276 KB |
Output is correct |
11 |
Correct |
14 ms |
29272 KB |
Output is correct |
12 |
Correct |
13 ms |
29276 KB |
Output is correct |
13 |
Correct |
14 ms |
29276 KB |
Output is correct |
14 |
Correct |
13 ms |
29276 KB |
Output is correct |
15 |
Correct |
13 ms |
29276 KB |
Output is correct |
16 |
Correct |
13 ms |
29276 KB |
Output is correct |
17 |
Correct |
13 ms |
29276 KB |
Output is correct |
18 |
Correct |
15 ms |
29276 KB |
Output is correct |
19 |
Correct |
16 ms |
29276 KB |
Output is correct |
20 |
Correct |
15 ms |
29276 KB |
Output is correct |
21 |
Correct |
16 ms |
29276 KB |
Output is correct |
22 |
Correct |
15 ms |
29276 KB |
Output is correct |
23 |
Correct |
15 ms |
29276 KB |
Output is correct |
24 |
Correct |
15 ms |
29276 KB |
Output is correct |
25 |
Correct |
13 ms |
29272 KB |
Output is correct |
26 |
Correct |
17 ms |
29276 KB |
Output is correct |
27 |
Correct |
17 ms |
29276 KB |
Output is correct |
28 |
Correct |
16 ms |
29276 KB |
Output is correct |
29 |
Correct |
16 ms |
29344 KB |
Output is correct |
30 |
Correct |
16 ms |
29276 KB |
Output is correct |
31 |
Correct |
17 ms |
29488 KB |
Output is correct |
32 |
Correct |
16 ms |
29272 KB |
Output is correct |
33 |
Correct |
16 ms |
29528 KB |
Output is correct |
34 |
Correct |
113 ms |
45496 KB |
Output is correct |
35 |
Correct |
119 ms |
49472 KB |
Output is correct |
36 |
Correct |
123 ms |
49516 KB |
Output is correct |
37 |
Correct |
172 ms |
49620 KB |
Output is correct |
38 |
Correct |
153 ms |
49464 KB |
Output is correct |
39 |
Correct |
148 ms |
49512 KB |
Output is correct |
40 |
Correct |
154 ms |
49508 KB |
Output is correct |
41 |
Correct |
14 ms |
29460 KB |
Output is correct |
42 |
Correct |
13 ms |
29272 KB |
Output is correct |
43 |
Correct |
161 ms |
36352 KB |
Output is correct |
44 |
Correct |
162 ms |
36416 KB |
Output is correct |
45 |
Correct |
204 ms |
36416 KB |
Output is correct |
46 |
Correct |
272 ms |
45060 KB |
Output is correct |
47 |
Correct |
302 ms |
44096 KB |
Output is correct |
48 |
Correct |
274 ms |
45744 KB |
Output is correct |
49 |
Correct |
293 ms |
44100 KB |
Output is correct |
50 |
Correct |
252 ms |
39744 KB |
Output is correct |
51 |
Correct |
244 ms |
39768 KB |
Output is correct |
52 |
Correct |
266 ms |
39580 KB |
Output is correct |
53 |
Correct |
259 ms |
39928 KB |
Output is correct |