#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])join(u,v);
for(int u=0;u<n;u++){
if(u!=v&&rep(u)==rep(v)){
// cout<<u<<' '<<v<<'\n';
dp[i]=max(dp[i],dp[p[u]]+dist(u,v));
}
}
// cout<<i<<' '<<dp[i]<<'\n';
}
cout<<dp[n]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5588 KB |
Output is correct |
3 |
Correct |
3 ms |
5712 KB |
Output is correct |
4 |
Correct |
3 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5712 KB |
Output is correct |
7 |
Correct |
3 ms |
5880 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 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 |
5588 KB |
Output is correct |
3 |
Correct |
3 ms |
5712 KB |
Output is correct |
4 |
Correct |
3 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5712 KB |
Output is correct |
7 |
Correct |
3 ms |
5880 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
7 ms |
5712 KB |
Output is correct |
12 |
Correct |
6 ms |
5712 KB |
Output is correct |
13 |
Correct |
6 ms |
5712 KB |
Output is correct |
14 |
Correct |
3 ms |
5712 KB |
Output is correct |
15 |
Correct |
3 ms |
5712 KB |
Output is correct |
16 |
Correct |
3 ms |
5944 KB |
Output is correct |
17 |
Correct |
4 ms |
5712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5588 KB |
Output is correct |
3 |
Correct |
3 ms |
5712 KB |
Output is correct |
4 |
Correct |
3 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5712 KB |
Output is correct |
7 |
Correct |
3 ms |
5880 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
7 ms |
5712 KB |
Output is correct |
12 |
Correct |
6 ms |
5712 KB |
Output is correct |
13 |
Correct |
6 ms |
5712 KB |
Output is correct |
14 |
Correct |
3 ms |
5712 KB |
Output is correct |
15 |
Correct |
3 ms |
5712 KB |
Output is correct |
16 |
Correct |
3 ms |
5944 KB |
Output is correct |
17 |
Correct |
4 ms |
5712 KB |
Output is correct |
18 |
Correct |
1302 ms |
6540 KB |
Output is correct |
19 |
Correct |
1183 ms |
6768 KB |
Output is correct |
20 |
Correct |
1188 ms |
6772 KB |
Output is correct |
21 |
Correct |
45 ms |
6736 KB |
Output is correct |
22 |
Correct |
46 ms |
6736 KB |
Output is correct |
23 |
Correct |
48 ms |
6736 KB |
Output is correct |
24 |
Correct |
46 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5588 KB |
Output is correct |
3 |
Correct |
3 ms |
5712 KB |
Output is correct |
4 |
Correct |
3 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5712 KB |
Output is correct |
7 |
Correct |
3 ms |
5880 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
7 ms |
5712 KB |
Output is correct |
12 |
Correct |
6 ms |
5712 KB |
Output is correct |
13 |
Correct |
6 ms |
5712 KB |
Output is correct |
14 |
Correct |
3 ms |
5712 KB |
Output is correct |
15 |
Correct |
3 ms |
5712 KB |
Output is correct |
16 |
Correct |
3 ms |
5944 KB |
Output is correct |
17 |
Correct |
4 ms |
5712 KB |
Output is correct |
18 |
Correct |
1302 ms |
6540 KB |
Output is correct |
19 |
Correct |
1183 ms |
6768 KB |
Output is correct |
20 |
Correct |
1188 ms |
6772 KB |
Output is correct |
21 |
Correct |
45 ms |
6736 KB |
Output is correct |
22 |
Correct |
46 ms |
6736 KB |
Output is correct |
23 |
Correct |
48 ms |
6736 KB |
Output is correct |
24 |
Correct |
46 ms |
6736 KB |
Output is correct |
25 |
Correct |
2 ms |
5712 KB |
Output is correct |
26 |
Correct |
1208 ms |
6620 KB |
Output is correct |
27 |
Correct |
1170 ms |
6660 KB |
Output is correct |
28 |
Correct |
1292 ms |
6608 KB |
Output is correct |
29 |
Correct |
1210 ms |
6692 KB |
Output is correct |
30 |
Correct |
105 ms |
6480 KB |
Output is correct |
31 |
Correct |
123 ms |
6496 KB |
Output is correct |
32 |
Correct |
108 ms |
6480 KB |
Output is correct |
33 |
Correct |
112 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5588 KB |
Output is correct |
3 |
Correct |
3 ms |
5712 KB |
Output is correct |
4 |
Correct |
3 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5712 KB |
Output is correct |
7 |
Correct |
3 ms |
5880 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
7 ms |
5712 KB |
Output is correct |
12 |
Correct |
6 ms |
5712 KB |
Output is correct |
13 |
Correct |
6 ms |
5712 KB |
Output is correct |
14 |
Correct |
3 ms |
5712 KB |
Output is correct |
15 |
Correct |
3 ms |
5712 KB |
Output is correct |
16 |
Correct |
3 ms |
5944 KB |
Output is correct |
17 |
Correct |
4 ms |
5712 KB |
Output is correct |
18 |
Correct |
1302 ms |
6540 KB |
Output is correct |
19 |
Correct |
1183 ms |
6768 KB |
Output is correct |
20 |
Correct |
1188 ms |
6772 KB |
Output is correct |
21 |
Correct |
45 ms |
6736 KB |
Output is correct |
22 |
Correct |
46 ms |
6736 KB |
Output is correct |
23 |
Correct |
48 ms |
6736 KB |
Output is correct |
24 |
Correct |
46 ms |
6736 KB |
Output is correct |
25 |
Execution timed out |
2078 ms |
46460 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5712 KB |
Output is correct |
2 |
Correct |
4 ms |
5800 KB |
Output is correct |
3 |
Execution timed out |
2062 ms |
35408 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5712 KB |
Output is correct |
2 |
Correct |
2 ms |
5588 KB |
Output is correct |
3 |
Correct |
3 ms |
5712 KB |
Output is correct |
4 |
Correct |
3 ms |
5712 KB |
Output is correct |
5 |
Correct |
2 ms |
5712 KB |
Output is correct |
6 |
Correct |
2 ms |
5712 KB |
Output is correct |
7 |
Correct |
3 ms |
5880 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
2 ms |
5712 KB |
Output is correct |
10 |
Correct |
2 ms |
5712 KB |
Output is correct |
11 |
Correct |
7 ms |
5712 KB |
Output is correct |
12 |
Correct |
6 ms |
5712 KB |
Output is correct |
13 |
Correct |
6 ms |
5712 KB |
Output is correct |
14 |
Correct |
3 ms |
5712 KB |
Output is correct |
15 |
Correct |
3 ms |
5712 KB |
Output is correct |
16 |
Correct |
3 ms |
5944 KB |
Output is correct |
17 |
Correct |
4 ms |
5712 KB |
Output is correct |
18 |
Correct |
1302 ms |
6540 KB |
Output is correct |
19 |
Correct |
1183 ms |
6768 KB |
Output is correct |
20 |
Correct |
1188 ms |
6772 KB |
Output is correct |
21 |
Correct |
45 ms |
6736 KB |
Output is correct |
22 |
Correct |
46 ms |
6736 KB |
Output is correct |
23 |
Correct |
48 ms |
6736 KB |
Output is correct |
24 |
Correct |
46 ms |
6736 KB |
Output is correct |
25 |
Correct |
2 ms |
5712 KB |
Output is correct |
26 |
Correct |
1208 ms |
6620 KB |
Output is correct |
27 |
Correct |
1170 ms |
6660 KB |
Output is correct |
28 |
Correct |
1292 ms |
6608 KB |
Output is correct |
29 |
Correct |
1210 ms |
6692 KB |
Output is correct |
30 |
Correct |
105 ms |
6480 KB |
Output is correct |
31 |
Correct |
123 ms |
6496 KB |
Output is correct |
32 |
Correct |
108 ms |
6480 KB |
Output is correct |
33 |
Correct |
112 ms |
6480 KB |
Output is correct |
34 |
Execution timed out |
2078 ms |
46460 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |