#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#define ll long long
#define pii pair<ll,ll>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
using namespace std;
const int mxn=2e5+5;
vector<int>g[mxn];
int pr[mxn]{0},h[mxn]{0},up[mxn][20]{0},d[mxn]{0};
void dfs(int u,int p){
up[u][0]=p;
for(int i=1;i<20;i++)up[u][i]=up[up[u][i-1]][i-1];
for(auto v:g[u]){
if(v==p)continue;
d[v]=d[u]+1;dfs(v,u);
}
}
int get(int r){
return pr[r]==r?r:pr[r]=get(pr[r]);
}
int lca(int a,int b){
if(d[b]<d[a])swap(a,b);
for(int i=19;i>=0;i--)if(d[b]-(1<<i)>=d[a])b=up[b][i];
if(a==b)return a;
for(int i=19;i>=0;i--)if(up[a][i]!=up[b][i])a=up[a][i],b=up[b][i];
return up[a][0];
}
int cal(int a,int b){
return d[a]+d[b]-2*d[lca(a,b)];
}
ll dp[mxn]{0};
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;iota(pr,pr+n+1,0);
for(int i=1;i<=n;i++)cin>>h[i];
for(int i=1;i<=n-1;i++){
int a,b;cin>>a>>b;
g[h[a]].pb(h[b]);g[h[b]].pb(h[a]);
}dfs(1,1);
for(int i=1;i<=n;i++){
for(auto v:g[i]){
int vv=get(v);
if(vv<i){
dp[i]=max(dp[i],dp[vv]+cal(i,vv));
pr[vv]=i;
}
}
}cout<<dp[n];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5160 KB |
Output is correct |
7 |
Correct |
2 ms |
5160 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5160 KB |
Output is correct |
7 |
Correct |
2 ms |
5160 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5160 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5160 KB |
Output is correct |
7 |
Correct |
2 ms |
5160 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5160 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5980 KB |
Output is correct |
19 |
Correct |
4 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
4 ms |
5980 KB |
Output is correct |
22 |
Correct |
4 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5980 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5160 KB |
Output is correct |
7 |
Correct |
2 ms |
5160 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5160 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5980 KB |
Output is correct |
19 |
Correct |
4 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
4 ms |
5980 KB |
Output is correct |
22 |
Correct |
4 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5980 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
4 ms |
5980 KB |
Output is correct |
27 |
Correct |
3 ms |
5980 KB |
Output is correct |
28 |
Correct |
3 ms |
5980 KB |
Output is correct |
29 |
Correct |
4 ms |
5980 KB |
Output is correct |
30 |
Correct |
4 ms |
5808 KB |
Output is correct |
31 |
Correct |
4 ms |
5724 KB |
Output is correct |
32 |
Correct |
4 ms |
5724 KB |
Output is correct |
33 |
Correct |
4 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5160 KB |
Output is correct |
7 |
Correct |
2 ms |
5160 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5160 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5980 KB |
Output is correct |
19 |
Correct |
4 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
4 ms |
5980 KB |
Output is correct |
22 |
Correct |
4 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5980 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
110 ms |
37892 KB |
Output is correct |
26 |
Correct |
78 ms |
41064 KB |
Output is correct |
27 |
Correct |
76 ms |
41136 KB |
Output is correct |
28 |
Correct |
122 ms |
40788 KB |
Output is correct |
29 |
Correct |
128 ms |
37968 KB |
Output is correct |
30 |
Correct |
119 ms |
40016 KB |
Output is correct |
31 |
Correct |
113 ms |
39508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Correct |
94 ms |
34392 KB |
Output is correct |
4 |
Correct |
102 ms |
34644 KB |
Output is correct |
5 |
Correct |
111 ms |
34640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
5160 KB |
Output is correct |
7 |
Correct |
2 ms |
5160 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5160 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
4 ms |
5980 KB |
Output is correct |
19 |
Correct |
4 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
4 ms |
5980 KB |
Output is correct |
22 |
Correct |
4 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5980 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
2 ms |
4956 KB |
Output is correct |
26 |
Correct |
4 ms |
5980 KB |
Output is correct |
27 |
Correct |
3 ms |
5980 KB |
Output is correct |
28 |
Correct |
3 ms |
5980 KB |
Output is correct |
29 |
Correct |
4 ms |
5980 KB |
Output is correct |
30 |
Correct |
4 ms |
5808 KB |
Output is correct |
31 |
Correct |
4 ms |
5724 KB |
Output is correct |
32 |
Correct |
4 ms |
5724 KB |
Output is correct |
33 |
Correct |
4 ms |
5724 KB |
Output is correct |
34 |
Correct |
110 ms |
37892 KB |
Output is correct |
35 |
Correct |
78 ms |
41064 KB |
Output is correct |
36 |
Correct |
76 ms |
41136 KB |
Output is correct |
37 |
Correct |
122 ms |
40788 KB |
Output is correct |
38 |
Correct |
128 ms |
37968 KB |
Output is correct |
39 |
Correct |
119 ms |
40016 KB |
Output is correct |
40 |
Correct |
113 ms |
39508 KB |
Output is correct |
41 |
Correct |
2 ms |
4956 KB |
Output is correct |
42 |
Correct |
2 ms |
5212 KB |
Output is correct |
43 |
Correct |
94 ms |
34392 KB |
Output is correct |
44 |
Correct |
102 ms |
34644 KB |
Output is correct |
45 |
Correct |
111 ms |
34640 KB |
Output is correct |
46 |
Correct |
91 ms |
39248 KB |
Output is correct |
47 |
Correct |
91 ms |
39248 KB |
Output is correct |
48 |
Correct |
90 ms |
39248 KB |
Output is correct |
49 |
Correct |
100 ms |
39248 KB |
Output is correct |
50 |
Correct |
118 ms |
34644 KB |
Output is correct |
51 |
Correct |
115 ms |
34644 KB |
Output is correct |
52 |
Correct |
114 ms |
34644 KB |
Output is correct |
53 |
Correct |
112 ms |
34644 KB |
Output is correct |