#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5;
vector<int> adj[maxn];
int sp[maxn][20],dt[maxn],par[maxn];
void dfs(int v,int p){
for(int u:adj[v])if(u!=p){
dt[u]=dt[v]+1;
sp[u][0]=v;
dfs(u,v);
}
}
int lca(int v1,int v2){
for(int i=19;i>-1;i--){
if(dt[v1]-(1<<i)>=dt[v2])v1=sp[v1][i];
if(dt[v2]-(1<<i)>=dt[v1])v2=sp[v2][i];
}
for(int i=19;i>-1;i--){
if(sp[v1][i]!=sp[v2][i]){
v1=sp[v1][i];
v2=sp[v2][i];
}
}
if(v1!=v2)v1=sp[v1][0];
return v1;
}
ll dist(int v1,int v2){
int x=lca(v1,v2);
return dt[v1]+dt[v2]-2*dt[x];
}
int rep(int v){
if(v==par[v])return v;
return par[v]=rep(par[v]);
}
void join(int x,int y){
x=rep(x);y=rep(y);
par[x]=y;
}
int main(){
int n;
cin>>n;
int p[n];
for(int i=0;i<n;i++)cin>>p[i];
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[--u].push_back(--v);
adj[v].push_back(u);
}
int idx[n+1];
for(int i=0;i<n;i++)idx[p[i]]=i;
dfs(0,0);
for(int i=1;i<20;i++){
for(int j=0;j<n;j++){
sp[j][i]=sp[sp[j][i-1]][i-1];
}
}
iota(par,par+n,0);
ll dp[n+1];
for(int i=1;i<=n;i++){
dp[i]=0;
int v=idx[i];
for(int u:adj[v])if(p[u]<p[v]){
u=rep(u);
join(u,v);
dp[i]=max(dp[i],dp[p[u]]+dist(u,v));
}
}
cout<<dp[n]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5712 KB |
Output is correct |
4 |
Correct |
2 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5884 KB |
Output is correct |
7 |
Correct |
2 ms |
5712 KB |
Output is correct |
8 |
Correct |
2 ms |
5712 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5712 KB |
Output is correct |
4 |
Correct |
2 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5884 KB |
Output is correct |
7 |
Correct |
2 ms |
5712 KB |
Output is correct |
8 |
Correct |
2 ms |
5712 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
2 ms |
5884 KB |
Output is correct |
12 |
Correct |
2 ms |
5712 KB |
Output is correct |
13 |
Correct |
3 ms |
5712 KB |
Output is correct |
14 |
Correct |
2 ms |
5712 KB |
Output is correct |
15 |
Correct |
2 ms |
5712 KB |
Output is correct |
16 |
Correct |
2 ms |
5712 KB |
Output is correct |
17 |
Correct |
2 ms |
5712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5712 KB |
Output is correct |
4 |
Correct |
2 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5884 KB |
Output is correct |
7 |
Correct |
2 ms |
5712 KB |
Output is correct |
8 |
Correct |
2 ms |
5712 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
2 ms |
5884 KB |
Output is correct |
12 |
Correct |
2 ms |
5712 KB |
Output is correct |
13 |
Correct |
3 ms |
5712 KB |
Output is correct |
14 |
Correct |
2 ms |
5712 KB |
Output is correct |
15 |
Correct |
2 ms |
5712 KB |
Output is correct |
16 |
Correct |
2 ms |
5712 KB |
Output is correct |
17 |
Correct |
2 ms |
5712 KB |
Output is correct |
18 |
Correct |
8 ms |
6480 KB |
Output is correct |
19 |
Correct |
6 ms |
6480 KB |
Output is correct |
20 |
Correct |
6 ms |
6644 KB |
Output is correct |
21 |
Correct |
7 ms |
6480 KB |
Output is correct |
22 |
Correct |
7 ms |
6480 KB |
Output is correct |
23 |
Correct |
6 ms |
6480 KB |
Output is correct |
24 |
Correct |
6 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5712 KB |
Output is correct |
4 |
Correct |
2 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5884 KB |
Output is correct |
7 |
Correct |
2 ms |
5712 KB |
Output is correct |
8 |
Correct |
2 ms |
5712 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
2 ms |
5884 KB |
Output is correct |
12 |
Correct |
2 ms |
5712 KB |
Output is correct |
13 |
Correct |
3 ms |
5712 KB |
Output is correct |
14 |
Correct |
2 ms |
5712 KB |
Output is correct |
15 |
Correct |
2 ms |
5712 KB |
Output is correct |
16 |
Correct |
2 ms |
5712 KB |
Output is correct |
17 |
Correct |
2 ms |
5712 KB |
Output is correct |
18 |
Correct |
8 ms |
6480 KB |
Output is correct |
19 |
Correct |
6 ms |
6480 KB |
Output is correct |
20 |
Correct |
6 ms |
6644 KB |
Output is correct |
21 |
Correct |
7 ms |
6480 KB |
Output is correct |
22 |
Correct |
7 ms |
6480 KB |
Output is correct |
23 |
Correct |
6 ms |
6480 KB |
Output is correct |
24 |
Correct |
6 ms |
6480 KB |
Output is correct |
25 |
Correct |
3 ms |
5712 KB |
Output is correct |
26 |
Correct |
7 ms |
6480 KB |
Output is correct |
27 |
Correct |
6 ms |
6480 KB |
Output is correct |
28 |
Correct |
6 ms |
6480 KB |
Output is correct |
29 |
Correct |
7 ms |
6480 KB |
Output is correct |
30 |
Correct |
7 ms |
6332 KB |
Output is correct |
31 |
Correct |
6 ms |
6392 KB |
Output is correct |
32 |
Correct |
6 ms |
6224 KB |
Output is correct |
33 |
Correct |
5 ms |
6224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5712 KB |
Output is correct |
4 |
Correct |
2 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5884 KB |
Output is correct |
7 |
Correct |
2 ms |
5712 KB |
Output is correct |
8 |
Correct |
2 ms |
5712 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
2 ms |
5884 KB |
Output is correct |
12 |
Correct |
2 ms |
5712 KB |
Output is correct |
13 |
Correct |
3 ms |
5712 KB |
Output is correct |
14 |
Correct |
2 ms |
5712 KB |
Output is correct |
15 |
Correct |
2 ms |
5712 KB |
Output is correct |
16 |
Correct |
2 ms |
5712 KB |
Output is correct |
17 |
Correct |
2 ms |
5712 KB |
Output is correct |
18 |
Correct |
8 ms |
6480 KB |
Output is correct |
19 |
Correct |
6 ms |
6480 KB |
Output is correct |
20 |
Correct |
6 ms |
6644 KB |
Output is correct |
21 |
Correct |
7 ms |
6480 KB |
Output is correct |
22 |
Correct |
7 ms |
6480 KB |
Output is correct |
23 |
Correct |
6 ms |
6480 KB |
Output is correct |
24 |
Correct |
6 ms |
6480 KB |
Output is correct |
25 |
Correct |
180 ms |
42568 KB |
Output is correct |
26 |
Correct |
166 ms |
46408 KB |
Output is correct |
27 |
Correct |
169 ms |
46344 KB |
Output is correct |
28 |
Correct |
185 ms |
46472 KB |
Output is correct |
29 |
Correct |
217 ms |
46320 KB |
Output is correct |
30 |
Correct |
192 ms |
46408 KB |
Output is correct |
31 |
Correct |
204 ms |
46408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
3 ms |
5712 KB |
Output is correct |
3 |
Correct |
212 ms |
31624 KB |
Output is correct |
4 |
Correct |
224 ms |
35400 KB |
Output is correct |
5 |
Correct |
215 ms |
35400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5712 KB |
Output is correct |
3 |
Correct |
2 ms |
5712 KB |
Output is correct |
4 |
Correct |
2 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5884 KB |
Output is correct |
7 |
Correct |
2 ms |
5712 KB |
Output is correct |
8 |
Correct |
2 ms |
5712 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
2 ms |
5884 KB |
Output is correct |
12 |
Correct |
2 ms |
5712 KB |
Output is correct |
13 |
Correct |
3 ms |
5712 KB |
Output is correct |
14 |
Correct |
2 ms |
5712 KB |
Output is correct |
15 |
Correct |
2 ms |
5712 KB |
Output is correct |
16 |
Correct |
2 ms |
5712 KB |
Output is correct |
17 |
Correct |
2 ms |
5712 KB |
Output is correct |
18 |
Correct |
8 ms |
6480 KB |
Output is correct |
19 |
Correct |
6 ms |
6480 KB |
Output is correct |
20 |
Correct |
6 ms |
6644 KB |
Output is correct |
21 |
Correct |
7 ms |
6480 KB |
Output is correct |
22 |
Correct |
7 ms |
6480 KB |
Output is correct |
23 |
Correct |
6 ms |
6480 KB |
Output is correct |
24 |
Correct |
6 ms |
6480 KB |
Output is correct |
25 |
Correct |
3 ms |
5712 KB |
Output is correct |
26 |
Correct |
7 ms |
6480 KB |
Output is correct |
27 |
Correct |
6 ms |
6480 KB |
Output is correct |
28 |
Correct |
6 ms |
6480 KB |
Output is correct |
29 |
Correct |
7 ms |
6480 KB |
Output is correct |
30 |
Correct |
7 ms |
6332 KB |
Output is correct |
31 |
Correct |
6 ms |
6392 KB |
Output is correct |
32 |
Correct |
6 ms |
6224 KB |
Output is correct |
33 |
Correct |
5 ms |
6224 KB |
Output is correct |
34 |
Correct |
180 ms |
42568 KB |
Output is correct |
35 |
Correct |
166 ms |
46408 KB |
Output is correct |
36 |
Correct |
169 ms |
46344 KB |
Output is correct |
37 |
Correct |
185 ms |
46472 KB |
Output is correct |
38 |
Correct |
217 ms |
46320 KB |
Output is correct |
39 |
Correct |
192 ms |
46408 KB |
Output is correct |
40 |
Correct |
204 ms |
46408 KB |
Output is correct |
41 |
Correct |
2 ms |
5712 KB |
Output is correct |
42 |
Correct |
3 ms |
5712 KB |
Output is correct |
43 |
Correct |
212 ms |
31624 KB |
Output is correct |
44 |
Correct |
224 ms |
35400 KB |
Output is correct |
45 |
Correct |
215 ms |
35400 KB |
Output is correct |
46 |
Correct |
262 ms |
40692 KB |
Output is correct |
47 |
Correct |
234 ms |
39752 KB |
Output is correct |
48 |
Correct |
283 ms |
41288 KB |
Output is correct |
49 |
Correct |
210 ms |
39752 KB |
Output is correct |
50 |
Correct |
234 ms |
35408 KB |
Output is correct |
51 |
Correct |
208 ms |
35448 KB |
Output is correct |
52 |
Correct |
261 ms |
35576 KB |
Output is correct |
53 |
Correct |
271 ms |
35576 KB |
Output is correct |