#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
const int N = 2e5 + 10;
const int LOG = 20;
int lift[N][LOG];
int depth[N];
int ar[N];
vector<int> v[N];
vector<array<int,2>> v2[N];
void dfs(int c,int p,int d){
depth[c]=d;
lift[c][0]=p;
for(int i=1;i<LOG;i++) lift[c][i]=lift[lift[c][i-1]][i-1];
for(int x:v[c]){
if(x==p) continue;
dfs(x,c,d+1);
}
}
int kth_par(int a,int x){
for(int i=0;i<LOG;i++) if(x>>i&1) a=lift[a][i];
return a;
}
int lca(int a,int b){
if(depth[a]<depth[b]) swap(a,b);
a=kth_par(a,depth[a]-depth[b]);
if(a==b) return a;
for(int i=LOG-1;i>=0;i--){
if(lift[a][i]!=lift[b][i]){
a=lift[a][i];
b=lift[b][i];
}
}
return lift[a][0];
}
int dist(int a,int b){
return depth[a] + depth[b] - 2*depth[lca(a,b)];
}
int par[N];
int siz[N];
int val[N];
int find(int a){
if(a==par[a]) return a;
return par[a]=find(par[a]);
}
void merge(int a,int b){
a=find(par[a]),b=find(par[b]);
if(a==b) return;
if(siz[a]>siz[b]) swap(a,b);
siz[b]+=siz[a];
par[a]=b;
if(ar[val[a]]>ar[val[b]]) val[b]=val[a];
}
int dp[N];
void dfs2(int c,int p){
for(auto x:v2[c]){
if(x[1]==p) continue;
dfs2(x[1],c);
dp[c]=max(dp[c],x[0]+dp[x[1]]);
}
// cout << "c: " << c << " " << dp[c] << endl;
}
void solve(){
int n;
cin >> n;
for(int i=1;i<=n;i++) cin >> ar[i];
int xd[n+1];
vector<int> act(n+1,0);
for(int i=1;i<=n;i++) xd[ar[i]]=i;
for(int i=1;i<n;i++){
int a,b;
cin >> a >> b;
v[a].pb(b);
v[b].pb(a);
}
for(int i=1;i<=n;i++){
val[i]=i;
siz[i]=1;
par[i]=i;
}
dfs(1,0,0);
for(int i=1;i<=n;i++){
int u = xd[i];
act[u] = 1;
for(int x:v[u]){
if(!act[x]) continue;
int w = val[find(x)];
v2[u].pb({dist(u,w),w});
v2[w].pb({dist(u,w),u});
// cout << "new: " << u << " " << w << " " << dist(u,w) << endl;
}
for(int x:v[u]){
if(!act[x]) continue;
merge(x,u);
}
}
dfs2(xd[n],0);
cout << dp[xd[n]] << endl;
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20828 KB |
Output is correct |
4 |
Correct |
4 ms |
20912 KB |
Output is correct |
5 |
Correct |
3 ms |
20980 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20928 KB |
Output is correct |
9 |
Correct |
3 ms |
20824 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20828 KB |
Output is correct |
4 |
Correct |
4 ms |
20912 KB |
Output is correct |
5 |
Correct |
3 ms |
20980 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20928 KB |
Output is correct |
9 |
Correct |
3 ms |
20824 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
20828 KB |
Output is correct |
12 |
Correct |
3 ms |
20828 KB |
Output is correct |
13 |
Correct |
3 ms |
20968 KB |
Output is correct |
14 |
Correct |
4 ms |
20916 KB |
Output is correct |
15 |
Correct |
4 ms |
20920 KB |
Output is correct |
16 |
Correct |
3 ms |
20828 KB |
Output is correct |
17 |
Correct |
4 ms |
20828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20828 KB |
Output is correct |
4 |
Correct |
4 ms |
20912 KB |
Output is correct |
5 |
Correct |
3 ms |
20980 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20928 KB |
Output is correct |
9 |
Correct |
3 ms |
20824 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
20828 KB |
Output is correct |
12 |
Correct |
3 ms |
20828 KB |
Output is correct |
13 |
Correct |
3 ms |
20968 KB |
Output is correct |
14 |
Correct |
4 ms |
20916 KB |
Output is correct |
15 |
Correct |
4 ms |
20920 KB |
Output is correct |
16 |
Correct |
3 ms |
20828 KB |
Output is correct |
17 |
Correct |
4 ms |
20828 KB |
Output is correct |
18 |
Correct |
6 ms |
21912 KB |
Output is correct |
19 |
Correct |
7 ms |
21852 KB |
Output is correct |
20 |
Correct |
6 ms |
21852 KB |
Output is correct |
21 |
Correct |
6 ms |
21744 KB |
Output is correct |
22 |
Correct |
6 ms |
21596 KB |
Output is correct |
23 |
Correct |
7 ms |
21788 KB |
Output is correct |
24 |
Correct |
6 ms |
21596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20828 KB |
Output is correct |
4 |
Correct |
4 ms |
20912 KB |
Output is correct |
5 |
Correct |
3 ms |
20980 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20928 KB |
Output is correct |
9 |
Correct |
3 ms |
20824 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
20828 KB |
Output is correct |
12 |
Correct |
3 ms |
20828 KB |
Output is correct |
13 |
Correct |
3 ms |
20968 KB |
Output is correct |
14 |
Correct |
4 ms |
20916 KB |
Output is correct |
15 |
Correct |
4 ms |
20920 KB |
Output is correct |
16 |
Correct |
3 ms |
20828 KB |
Output is correct |
17 |
Correct |
4 ms |
20828 KB |
Output is correct |
18 |
Correct |
6 ms |
21912 KB |
Output is correct |
19 |
Correct |
7 ms |
21852 KB |
Output is correct |
20 |
Correct |
6 ms |
21852 KB |
Output is correct |
21 |
Correct |
6 ms |
21744 KB |
Output is correct |
22 |
Correct |
6 ms |
21596 KB |
Output is correct |
23 |
Correct |
7 ms |
21788 KB |
Output is correct |
24 |
Correct |
6 ms |
21596 KB |
Output is correct |
25 |
Correct |
3 ms |
20828 KB |
Output is correct |
26 |
Correct |
6 ms |
21852 KB |
Output is correct |
27 |
Correct |
7 ms |
21860 KB |
Output is correct |
28 |
Correct |
6 ms |
21848 KB |
Output is correct |
29 |
Correct |
6 ms |
21852 KB |
Output is correct |
30 |
Correct |
6 ms |
21596 KB |
Output is correct |
31 |
Correct |
6 ms |
21592 KB |
Output is correct |
32 |
Correct |
7 ms |
21548 KB |
Output is correct |
33 |
Correct |
7 ms |
21596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20828 KB |
Output is correct |
4 |
Correct |
4 ms |
20912 KB |
Output is correct |
5 |
Correct |
3 ms |
20980 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20928 KB |
Output is correct |
9 |
Correct |
3 ms |
20824 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
20828 KB |
Output is correct |
12 |
Correct |
3 ms |
20828 KB |
Output is correct |
13 |
Correct |
3 ms |
20968 KB |
Output is correct |
14 |
Correct |
4 ms |
20916 KB |
Output is correct |
15 |
Correct |
4 ms |
20920 KB |
Output is correct |
16 |
Correct |
3 ms |
20828 KB |
Output is correct |
17 |
Correct |
4 ms |
20828 KB |
Output is correct |
18 |
Correct |
6 ms |
21912 KB |
Output is correct |
19 |
Correct |
7 ms |
21852 KB |
Output is correct |
20 |
Correct |
6 ms |
21852 KB |
Output is correct |
21 |
Correct |
6 ms |
21744 KB |
Output is correct |
22 |
Correct |
6 ms |
21596 KB |
Output is correct |
23 |
Correct |
7 ms |
21788 KB |
Output is correct |
24 |
Correct |
6 ms |
21596 KB |
Output is correct |
25 |
Correct |
153 ms |
88556 KB |
Output is correct |
26 |
Correct |
156 ms |
87504 KB |
Output is correct |
27 |
Correct |
143 ms |
87508 KB |
Output is correct |
28 |
Correct |
215 ms |
83788 KB |
Output is correct |
29 |
Correct |
223 ms |
83804 KB |
Output is correct |
30 |
Correct |
219 ms |
84036 KB |
Output is correct |
31 |
Correct |
235 ms |
83728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
242 ms |
76028 KB |
Output is correct |
4 |
Correct |
227 ms |
76112 KB |
Output is correct |
5 |
Correct |
239 ms |
75860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20828 KB |
Output is correct |
4 |
Correct |
4 ms |
20912 KB |
Output is correct |
5 |
Correct |
3 ms |
20980 KB |
Output is correct |
6 |
Correct |
3 ms |
20828 KB |
Output is correct |
7 |
Correct |
3 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20928 KB |
Output is correct |
9 |
Correct |
3 ms |
20824 KB |
Output is correct |
10 |
Correct |
3 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
20828 KB |
Output is correct |
12 |
Correct |
3 ms |
20828 KB |
Output is correct |
13 |
Correct |
3 ms |
20968 KB |
Output is correct |
14 |
Correct |
4 ms |
20916 KB |
Output is correct |
15 |
Correct |
4 ms |
20920 KB |
Output is correct |
16 |
Correct |
3 ms |
20828 KB |
Output is correct |
17 |
Correct |
4 ms |
20828 KB |
Output is correct |
18 |
Correct |
6 ms |
21912 KB |
Output is correct |
19 |
Correct |
7 ms |
21852 KB |
Output is correct |
20 |
Correct |
6 ms |
21852 KB |
Output is correct |
21 |
Correct |
6 ms |
21744 KB |
Output is correct |
22 |
Correct |
6 ms |
21596 KB |
Output is correct |
23 |
Correct |
7 ms |
21788 KB |
Output is correct |
24 |
Correct |
6 ms |
21596 KB |
Output is correct |
25 |
Correct |
3 ms |
20828 KB |
Output is correct |
26 |
Correct |
6 ms |
21852 KB |
Output is correct |
27 |
Correct |
7 ms |
21860 KB |
Output is correct |
28 |
Correct |
6 ms |
21848 KB |
Output is correct |
29 |
Correct |
6 ms |
21852 KB |
Output is correct |
30 |
Correct |
6 ms |
21596 KB |
Output is correct |
31 |
Correct |
6 ms |
21592 KB |
Output is correct |
32 |
Correct |
7 ms |
21548 KB |
Output is correct |
33 |
Correct |
7 ms |
21596 KB |
Output is correct |
34 |
Correct |
153 ms |
88556 KB |
Output is correct |
35 |
Correct |
156 ms |
87504 KB |
Output is correct |
36 |
Correct |
143 ms |
87508 KB |
Output is correct |
37 |
Correct |
215 ms |
83788 KB |
Output is correct |
38 |
Correct |
223 ms |
83804 KB |
Output is correct |
39 |
Correct |
219 ms |
84036 KB |
Output is correct |
40 |
Correct |
235 ms |
83728 KB |
Output is correct |
41 |
Correct |
3 ms |
20828 KB |
Output is correct |
42 |
Correct |
4 ms |
20828 KB |
Output is correct |
43 |
Correct |
242 ms |
76028 KB |
Output is correct |
44 |
Correct |
227 ms |
76112 KB |
Output is correct |
45 |
Correct |
239 ms |
75860 KB |
Output is correct |
46 |
Correct |
299 ms |
84988 KB |
Output is correct |
47 |
Correct |
323 ms |
84928 KB |
Output is correct |
48 |
Correct |
316 ms |
84812 KB |
Output is correct |
49 |
Correct |
296 ms |
84888 KB |
Output is correct |
50 |
Correct |
289 ms |
75004 KB |
Output is correct |
51 |
Correct |
318 ms |
75088 KB |
Output is correct |
52 |
Correct |
315 ms |
74832 KB |
Output is correct |
53 |
Correct |
302 ms |
75092 KB |
Output is correct |