#include "bits/stdc++.h"
using namespace std;
const int N=2e5, L=18;
#ifndef LOCAL
#define cerr if(0)cerr
#endif
int n,p[N];
int pp[L][N],d[N];
vector<int>g[N];
long long dp[N];
int f[N];
int uf(int i) { return f[i]==i?i:f[i]=uf(f[i]); }
void dfs(int i){
for(int l=1;l<L;l++)pp[l][i]=pp[l-1][pp[l-1][i]];
for(int j:g[i])if(pp[0][i]!=j){
pp[0][j]=i;
d[j]=d[i]+1;
dfs(j);
}
}
int lca(int i,int j){
if(d[i]<d[j])swap(i,j);
int k=d[i]-d[j];
for(int l=0;l<L;l++)if(k>>l&1)i=pp[l][i];
if(i==j)return i;
for(int l=L-1;l>=0;l--)if(pp[l][i]!=pp[l][j])i=pp[l][i],j=pp[l][j];
return pp[0][i];
}
int dis(int i,int j){return d[i]+d[j]-2*d[lca(i,j)];}
int main() {
cin.tie(0)->sync_with_stdio(0),cin.exceptions(cin.failbit);
cin>>n;
for(int i=0;i<n;i++)cin>>p[i],p[i]--;
for(int h=0;h<n-1;h++){
int i,j;cin>>i>>j,i--,j--;
i=p[i],j=p[j];
cerr<<"edge "<<i<<" "<<j<<endl;
g[i].push_back(j);
g[j].push_back(i);
}
pp[0][0]=0;
dfs(0);
for(int i=0;i<n;i++)f[i]=i;
for(int i=0;i<n;i++){
for(int j:g[i])if(j<i){
int c=uf(j);
cerr<<c<<" "<<i<<"|"<<dis(c,i)<<endl;
dp[i]=max(dp[i],dp[c]+dis(c,i));
f[c]=i;
}
cerr<<"dp["<<i<<"]="<<dp[i]<<endl;
}
cout<<*max_element(dp,dp+n)<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5040 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5040 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
4 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
6028 KB |
Output is correct |
20 |
Correct |
4 ms |
6100 KB |
Output is correct |
21 |
Correct |
4 ms |
5876 KB |
Output is correct |
22 |
Correct |
4 ms |
6100 KB |
Output is correct |
23 |
Correct |
5 ms |
5972 KB |
Output is correct |
24 |
Correct |
5 ms |
5972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5040 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
4 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
6028 KB |
Output is correct |
20 |
Correct |
4 ms |
6100 KB |
Output is correct |
21 |
Correct |
4 ms |
5876 KB |
Output is correct |
22 |
Correct |
4 ms |
6100 KB |
Output is correct |
23 |
Correct |
5 ms |
5972 KB |
Output is correct |
24 |
Correct |
5 ms |
5972 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5972 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
4 ms |
5972 KB |
Output is correct |
29 |
Correct |
6 ms |
5972 KB |
Output is correct |
30 |
Correct |
5 ms |
5760 KB |
Output is correct |
31 |
Correct |
6 ms |
5812 KB |
Output is correct |
32 |
Correct |
4 ms |
5716 KB |
Output is correct |
33 |
Correct |
5 ms |
5804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5040 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
4 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
6028 KB |
Output is correct |
20 |
Correct |
4 ms |
6100 KB |
Output is correct |
21 |
Correct |
4 ms |
5876 KB |
Output is correct |
22 |
Correct |
4 ms |
6100 KB |
Output is correct |
23 |
Correct |
5 ms |
5972 KB |
Output is correct |
24 |
Correct |
5 ms |
5972 KB |
Output is correct |
25 |
Correct |
86 ms |
39276 KB |
Output is correct |
26 |
Correct |
92 ms |
45432 KB |
Output is correct |
27 |
Correct |
99 ms |
45376 KB |
Output is correct |
28 |
Correct |
192 ms |
44880 KB |
Output is correct |
29 |
Correct |
159 ms |
39408 KB |
Output is correct |
30 |
Correct |
164 ms |
43288 KB |
Output is correct |
31 |
Correct |
170 ms |
42204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5164 KB |
Output is correct |
3 |
Correct |
183 ms |
32908 KB |
Output is correct |
4 |
Correct |
162 ms |
32932 KB |
Output is correct |
5 |
Correct |
152 ms |
32828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5076 KB |
Output is correct |
7 |
Correct |
2 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5040 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5160 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
4 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
6028 KB |
Output is correct |
20 |
Correct |
4 ms |
6100 KB |
Output is correct |
21 |
Correct |
4 ms |
5876 KB |
Output is correct |
22 |
Correct |
4 ms |
6100 KB |
Output is correct |
23 |
Correct |
5 ms |
5972 KB |
Output is correct |
24 |
Correct |
5 ms |
5972 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5972 KB |
Output is correct |
27 |
Correct |
4 ms |
5972 KB |
Output is correct |
28 |
Correct |
4 ms |
5972 KB |
Output is correct |
29 |
Correct |
6 ms |
5972 KB |
Output is correct |
30 |
Correct |
5 ms |
5760 KB |
Output is correct |
31 |
Correct |
6 ms |
5812 KB |
Output is correct |
32 |
Correct |
4 ms |
5716 KB |
Output is correct |
33 |
Correct |
5 ms |
5804 KB |
Output is correct |
34 |
Correct |
86 ms |
39276 KB |
Output is correct |
35 |
Correct |
92 ms |
45432 KB |
Output is correct |
36 |
Correct |
99 ms |
45376 KB |
Output is correct |
37 |
Correct |
192 ms |
44880 KB |
Output is correct |
38 |
Correct |
159 ms |
39408 KB |
Output is correct |
39 |
Correct |
164 ms |
43288 KB |
Output is correct |
40 |
Correct |
170 ms |
42204 KB |
Output is correct |
41 |
Correct |
2 ms |
5076 KB |
Output is correct |
42 |
Correct |
2 ms |
5164 KB |
Output is correct |
43 |
Correct |
183 ms |
32908 KB |
Output is correct |
44 |
Correct |
162 ms |
32932 KB |
Output is correct |
45 |
Correct |
152 ms |
32828 KB |
Output is correct |
46 |
Correct |
116 ms |
41716 KB |
Output is correct |
47 |
Correct |
124 ms |
41708 KB |
Output is correct |
48 |
Correct |
120 ms |
41808 KB |
Output is correct |
49 |
Correct |
120 ms |
41716 KB |
Output is correct |
50 |
Correct |
193 ms |
33100 KB |
Output is correct |
51 |
Correct |
184 ms |
33124 KB |
Output is correct |
52 |
Correct |
191 ms |
33228 KB |
Output is correct |
53 |
Correct |
210 ms |
33124 KB |
Output is correct |