#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=2e5+5;
int a[N],d[N],p[N],dp[N];
vector <int> g[N],g2[N];
int jmp[20][N];
void dfs(int v,int p){
jmp[0][v]=p;
for(auto to : g[v]){
if(to!=p){
d[to]=d[v]+1;
dfs(to,v);
}
}
}
int lca(int v,int u){
if(d[v]<d[u])swap(u,v);
for(int k=19;k>=0;k--){
if(d[jmp[k][v]]>=d[u]){
v=jmp[k][v];
}
}
if(u==v)return v;
for(int k=19;k>=0;k--){
if(jmp[k][v]!=jmp[k][u]){
v=jmp[k][v];
u=jmp[k][u];
}
}
return jmp[0][v];
}
int find_set(int v){
if(p[v]==v)return v;
return p[v]=find_set(p[v]);
}
void union_set(int v,int u){
u=find_set(u);
p[u]=v;
g2[v].pb(u);
}
int dist(int v,int u){
int l=lca(u,v);
return d[u]+d[v]-2*d[l];
}
void dfs2(int v,int p){
for(auto to : g2[v]){
if(to!=p){
dfs2(to,v);
dp[v]=max(dp[to]+dist(to,v),dp[v]);
}
}
}
signed main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
p[i]=i;
}
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
g[a[u]].pb(a[v]);
g[a[v]].pb(a[u]);
}
dfs(n,n);
for(int k=1;k<20;k++){
for(int i=1;i<=n;i++){
jmp[k][i]=jmp[k-1][jmp[k-1][i]];
}
}
for(int i=2;i<=n;i++){
for(auto to : g[i]){
if(to<i){
union_set(i,to);
}
}
}
dfs2(n,0);
cout<<dp[n]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
43356 KB |
Output is correct |
2 |
Correct |
5 ms |
43356 KB |
Output is correct |
3 |
Correct |
5 ms |
43356 KB |
Output is correct |
4 |
Correct |
5 ms |
43356 KB |
Output is correct |
5 |
Correct |
5 ms |
43368 KB |
Output is correct |
6 |
Correct |
5 ms |
43532 KB |
Output is correct |
7 |
Correct |
5 ms |
43356 KB |
Output is correct |
8 |
Correct |
5 ms |
43548 KB |
Output is correct |
9 |
Correct |
5 ms |
43356 KB |
Output is correct |
10 |
Correct |
5 ms |
43464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
43356 KB |
Output is correct |
2 |
Correct |
5 ms |
43356 KB |
Output is correct |
3 |
Correct |
5 ms |
43356 KB |
Output is correct |
4 |
Correct |
5 ms |
43356 KB |
Output is correct |
5 |
Correct |
5 ms |
43368 KB |
Output is correct |
6 |
Correct |
5 ms |
43532 KB |
Output is correct |
7 |
Correct |
5 ms |
43356 KB |
Output is correct |
8 |
Correct |
5 ms |
43548 KB |
Output is correct |
9 |
Correct |
5 ms |
43356 KB |
Output is correct |
10 |
Correct |
5 ms |
43464 KB |
Output is correct |
11 |
Correct |
6 ms |
43352 KB |
Output is correct |
12 |
Correct |
5 ms |
43356 KB |
Output is correct |
13 |
Correct |
5 ms |
43356 KB |
Output is correct |
14 |
Correct |
6 ms |
43352 KB |
Output is correct |
15 |
Correct |
5 ms |
43356 KB |
Output is correct |
16 |
Correct |
5 ms |
43548 KB |
Output is correct |
17 |
Correct |
5 ms |
43356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
43356 KB |
Output is correct |
2 |
Correct |
5 ms |
43356 KB |
Output is correct |
3 |
Correct |
5 ms |
43356 KB |
Output is correct |
4 |
Correct |
5 ms |
43356 KB |
Output is correct |
5 |
Correct |
5 ms |
43368 KB |
Output is correct |
6 |
Correct |
5 ms |
43532 KB |
Output is correct |
7 |
Correct |
5 ms |
43356 KB |
Output is correct |
8 |
Correct |
5 ms |
43548 KB |
Output is correct |
9 |
Correct |
5 ms |
43356 KB |
Output is correct |
10 |
Correct |
5 ms |
43464 KB |
Output is correct |
11 |
Correct |
6 ms |
43352 KB |
Output is correct |
12 |
Correct |
5 ms |
43356 KB |
Output is correct |
13 |
Correct |
5 ms |
43356 KB |
Output is correct |
14 |
Correct |
6 ms |
43352 KB |
Output is correct |
15 |
Correct |
5 ms |
43356 KB |
Output is correct |
16 |
Correct |
5 ms |
43548 KB |
Output is correct |
17 |
Correct |
5 ms |
43356 KB |
Output is correct |
18 |
Correct |
8 ms |
44380 KB |
Output is correct |
19 |
Correct |
8 ms |
44380 KB |
Output is correct |
20 |
Correct |
9 ms |
44380 KB |
Output is correct |
21 |
Correct |
8 ms |
44180 KB |
Output is correct |
22 |
Correct |
8 ms |
44124 KB |
Output is correct |
23 |
Correct |
8 ms |
44124 KB |
Output is correct |
24 |
Correct |
9 ms |
43868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
43356 KB |
Output is correct |
2 |
Correct |
5 ms |
43356 KB |
Output is correct |
3 |
Correct |
5 ms |
43356 KB |
Output is correct |
4 |
Correct |
5 ms |
43356 KB |
Output is correct |
5 |
Correct |
5 ms |
43368 KB |
Output is correct |
6 |
Correct |
5 ms |
43532 KB |
Output is correct |
7 |
Correct |
5 ms |
43356 KB |
Output is correct |
8 |
Correct |
5 ms |
43548 KB |
Output is correct |
9 |
Correct |
5 ms |
43356 KB |
Output is correct |
10 |
Correct |
5 ms |
43464 KB |
Output is correct |
11 |
Correct |
6 ms |
43352 KB |
Output is correct |
12 |
Correct |
5 ms |
43356 KB |
Output is correct |
13 |
Correct |
5 ms |
43356 KB |
Output is correct |
14 |
Correct |
6 ms |
43352 KB |
Output is correct |
15 |
Correct |
5 ms |
43356 KB |
Output is correct |
16 |
Correct |
5 ms |
43548 KB |
Output is correct |
17 |
Correct |
5 ms |
43356 KB |
Output is correct |
18 |
Correct |
8 ms |
44380 KB |
Output is correct |
19 |
Correct |
8 ms |
44380 KB |
Output is correct |
20 |
Correct |
9 ms |
44380 KB |
Output is correct |
21 |
Correct |
8 ms |
44180 KB |
Output is correct |
22 |
Correct |
8 ms |
44124 KB |
Output is correct |
23 |
Correct |
8 ms |
44124 KB |
Output is correct |
24 |
Correct |
9 ms |
43868 KB |
Output is correct |
25 |
Correct |
5 ms |
43356 KB |
Output is correct |
26 |
Correct |
8 ms |
44308 KB |
Output is correct |
27 |
Correct |
8 ms |
44192 KB |
Output is correct |
28 |
Correct |
8 ms |
44124 KB |
Output is correct |
29 |
Correct |
9 ms |
44124 KB |
Output is correct |
30 |
Correct |
8 ms |
43868 KB |
Output is correct |
31 |
Correct |
11 ms |
43868 KB |
Output is correct |
32 |
Correct |
10 ms |
44000 KB |
Output is correct |
33 |
Correct |
9 ms |
43868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
43356 KB |
Output is correct |
2 |
Correct |
5 ms |
43356 KB |
Output is correct |
3 |
Correct |
5 ms |
43356 KB |
Output is correct |
4 |
Correct |
5 ms |
43356 KB |
Output is correct |
5 |
Correct |
5 ms |
43368 KB |
Output is correct |
6 |
Correct |
5 ms |
43532 KB |
Output is correct |
7 |
Correct |
5 ms |
43356 KB |
Output is correct |
8 |
Correct |
5 ms |
43548 KB |
Output is correct |
9 |
Correct |
5 ms |
43356 KB |
Output is correct |
10 |
Correct |
5 ms |
43464 KB |
Output is correct |
11 |
Correct |
6 ms |
43352 KB |
Output is correct |
12 |
Correct |
5 ms |
43356 KB |
Output is correct |
13 |
Correct |
5 ms |
43356 KB |
Output is correct |
14 |
Correct |
6 ms |
43352 KB |
Output is correct |
15 |
Correct |
5 ms |
43356 KB |
Output is correct |
16 |
Correct |
5 ms |
43548 KB |
Output is correct |
17 |
Correct |
5 ms |
43356 KB |
Output is correct |
18 |
Correct |
8 ms |
44380 KB |
Output is correct |
19 |
Correct |
8 ms |
44380 KB |
Output is correct |
20 |
Correct |
9 ms |
44380 KB |
Output is correct |
21 |
Correct |
8 ms |
44180 KB |
Output is correct |
22 |
Correct |
8 ms |
44124 KB |
Output is correct |
23 |
Correct |
8 ms |
44124 KB |
Output is correct |
24 |
Correct |
9 ms |
43868 KB |
Output is correct |
25 |
Correct |
190 ms |
79280 KB |
Output is correct |
26 |
Correct |
178 ms |
77140 KB |
Output is correct |
27 |
Correct |
176 ms |
77136 KB |
Output is correct |
28 |
Correct |
223 ms |
68436 KB |
Output is correct |
29 |
Correct |
237 ms |
69688 KB |
Output is correct |
30 |
Correct |
237 ms |
69488 KB |
Output is correct |
31 |
Correct |
239 ms |
69972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
43356 KB |
Output is correct |
2 |
Correct |
5 ms |
43356 KB |
Output is correct |
3 |
Correct |
238 ms |
64176 KB |
Output is correct |
4 |
Correct |
225 ms |
64592 KB |
Output is correct |
5 |
Correct |
274 ms |
64160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
43356 KB |
Output is correct |
2 |
Correct |
5 ms |
43356 KB |
Output is correct |
3 |
Correct |
5 ms |
43356 KB |
Output is correct |
4 |
Correct |
5 ms |
43356 KB |
Output is correct |
5 |
Correct |
5 ms |
43368 KB |
Output is correct |
6 |
Correct |
5 ms |
43532 KB |
Output is correct |
7 |
Correct |
5 ms |
43356 KB |
Output is correct |
8 |
Correct |
5 ms |
43548 KB |
Output is correct |
9 |
Correct |
5 ms |
43356 KB |
Output is correct |
10 |
Correct |
5 ms |
43464 KB |
Output is correct |
11 |
Correct |
6 ms |
43352 KB |
Output is correct |
12 |
Correct |
5 ms |
43356 KB |
Output is correct |
13 |
Correct |
5 ms |
43356 KB |
Output is correct |
14 |
Correct |
6 ms |
43352 KB |
Output is correct |
15 |
Correct |
5 ms |
43356 KB |
Output is correct |
16 |
Correct |
5 ms |
43548 KB |
Output is correct |
17 |
Correct |
5 ms |
43356 KB |
Output is correct |
18 |
Correct |
8 ms |
44380 KB |
Output is correct |
19 |
Correct |
8 ms |
44380 KB |
Output is correct |
20 |
Correct |
9 ms |
44380 KB |
Output is correct |
21 |
Correct |
8 ms |
44180 KB |
Output is correct |
22 |
Correct |
8 ms |
44124 KB |
Output is correct |
23 |
Correct |
8 ms |
44124 KB |
Output is correct |
24 |
Correct |
9 ms |
43868 KB |
Output is correct |
25 |
Correct |
5 ms |
43356 KB |
Output is correct |
26 |
Correct |
8 ms |
44308 KB |
Output is correct |
27 |
Correct |
8 ms |
44192 KB |
Output is correct |
28 |
Correct |
8 ms |
44124 KB |
Output is correct |
29 |
Correct |
9 ms |
44124 KB |
Output is correct |
30 |
Correct |
8 ms |
43868 KB |
Output is correct |
31 |
Correct |
11 ms |
43868 KB |
Output is correct |
32 |
Correct |
10 ms |
44000 KB |
Output is correct |
33 |
Correct |
9 ms |
43868 KB |
Output is correct |
34 |
Correct |
190 ms |
79280 KB |
Output is correct |
35 |
Correct |
178 ms |
77140 KB |
Output is correct |
36 |
Correct |
176 ms |
77136 KB |
Output is correct |
37 |
Correct |
223 ms |
68436 KB |
Output is correct |
38 |
Correct |
237 ms |
69688 KB |
Output is correct |
39 |
Correct |
237 ms |
69488 KB |
Output is correct |
40 |
Correct |
239 ms |
69972 KB |
Output is correct |
41 |
Correct |
5 ms |
43356 KB |
Output is correct |
42 |
Correct |
5 ms |
43356 KB |
Output is correct |
43 |
Correct |
238 ms |
64176 KB |
Output is correct |
44 |
Correct |
225 ms |
64592 KB |
Output is correct |
45 |
Correct |
274 ms |
64160 KB |
Output is correct |
46 |
Correct |
189 ms |
74196 KB |
Output is correct |
47 |
Correct |
219 ms |
74068 KB |
Output is correct |
48 |
Correct |
198 ms |
74064 KB |
Output is correct |
49 |
Correct |
182 ms |
74064 KB |
Output is correct |
50 |
Correct |
245 ms |
62880 KB |
Output is correct |
51 |
Correct |
235 ms |
62804 KB |
Output is correct |
52 |
Correct |
236 ms |
62800 KB |
Output is correct |
53 |
Correct |
248 ms |
62916 KB |
Output is correct |