#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<cmath>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define pll pair<ll,ll>
#define pb push_back
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define double long double
#define mod 1000000007
const int mxn=2*1e5,lg=25;
int pa[mxn+10],n;
vector<int>adj[mxn+10];
int he[mxn+10],h[mxn+10],up[mxn+10][lg+10];
void dfs(int cur,int p){
for(auto i:adj[cur]){
if(i==p)continue;
up[i][0]=cur;
h[i]=h[cur]+1;
dfs(i,cur);
}
}
int lca(int a,int b){
if(h[a]<h[b])swap(a,b);
int k=h[a]-h[b];
for(int i=0;i<=lg;i++)if(k&(1<<i))a=up[a][i];
if(a==b)return a;
for(int i=lg;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 h[a]+h[b]-(2*h[lca(a,b)]);
}
int find(int u){
if(pa[u]==u)return u;
return pa[u]=find(pa[u]);
}
int dp[mxn+10];//max
int32_t main(){
fastio
cin>>n;
for(int i=1;i<=n;i++){
int a;cin>>a;
he[i]=a;
pa[i]=i;
}
for(int i=0;i<n-1;i++){
int a,b;cin>>a>>b;
adj[he[a]].pb(he[b]);//id by height
adj[he[b]].pb(he[a]);
}
dfs(n,-1);
for(int i=1;i<=lg;i++)for(int j=1;j<=n;j++)up[j][i]=up[up[j][i-1]][i-1];
for(int i=1;i<=n;i++){
for(auto j:adj[i]){
int c=find(j);
if(c<i){
dp[i]=max(dp[i],dp[c]+cal(i,c));
pa[c]=i;
}
}
}
cout<<dp[n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9048 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9048 KB |
Output is correct |
8 |
Correct |
3 ms |
9196 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9048 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9048 KB |
Output is correct |
8 |
Correct |
3 ms |
9196 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9048 KB |
Output is correct |
11 |
Correct |
2 ms |
9048 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9048 KB |
Output is correct |
14 |
Correct |
2 ms |
9048 KB |
Output is correct |
15 |
Correct |
2 ms |
9048 KB |
Output is correct |
16 |
Correct |
2 ms |
9052 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9048 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9048 KB |
Output is correct |
8 |
Correct |
3 ms |
9196 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9048 KB |
Output is correct |
11 |
Correct |
2 ms |
9048 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9048 KB |
Output is correct |
14 |
Correct |
2 ms |
9048 KB |
Output is correct |
15 |
Correct |
2 ms |
9048 KB |
Output is correct |
16 |
Correct |
2 ms |
9052 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
18 |
Correct |
4 ms |
11724 KB |
Output is correct |
19 |
Correct |
4 ms |
11612 KB |
Output is correct |
20 |
Correct |
4 ms |
11608 KB |
Output is correct |
21 |
Correct |
4 ms |
11608 KB |
Output is correct |
22 |
Correct |
4 ms |
11608 KB |
Output is correct |
23 |
Correct |
5 ms |
11640 KB |
Output is correct |
24 |
Correct |
4 ms |
11608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9048 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9048 KB |
Output is correct |
8 |
Correct |
3 ms |
9196 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9048 KB |
Output is correct |
11 |
Correct |
2 ms |
9048 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9048 KB |
Output is correct |
14 |
Correct |
2 ms |
9048 KB |
Output is correct |
15 |
Correct |
2 ms |
9048 KB |
Output is correct |
16 |
Correct |
2 ms |
9052 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
18 |
Correct |
4 ms |
11724 KB |
Output is correct |
19 |
Correct |
4 ms |
11612 KB |
Output is correct |
20 |
Correct |
4 ms |
11608 KB |
Output is correct |
21 |
Correct |
4 ms |
11608 KB |
Output is correct |
22 |
Correct |
4 ms |
11608 KB |
Output is correct |
23 |
Correct |
5 ms |
11640 KB |
Output is correct |
24 |
Correct |
4 ms |
11608 KB |
Output is correct |
25 |
Correct |
2 ms |
9048 KB |
Output is correct |
26 |
Correct |
4 ms |
11612 KB |
Output is correct |
27 |
Correct |
4 ms |
11608 KB |
Output is correct |
28 |
Correct |
4 ms |
11608 KB |
Output is correct |
29 |
Correct |
4 ms |
11608 KB |
Output is correct |
30 |
Correct |
4 ms |
11356 KB |
Output is correct |
31 |
Correct |
4 ms |
11356 KB |
Output is correct |
32 |
Correct |
4 ms |
11356 KB |
Output is correct |
33 |
Correct |
4 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9048 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9048 KB |
Output is correct |
8 |
Correct |
3 ms |
9196 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9048 KB |
Output is correct |
11 |
Correct |
2 ms |
9048 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9048 KB |
Output is correct |
14 |
Correct |
2 ms |
9048 KB |
Output is correct |
15 |
Correct |
2 ms |
9048 KB |
Output is correct |
16 |
Correct |
2 ms |
9052 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
18 |
Correct |
4 ms |
11724 KB |
Output is correct |
19 |
Correct |
4 ms |
11612 KB |
Output is correct |
20 |
Correct |
4 ms |
11608 KB |
Output is correct |
21 |
Correct |
4 ms |
11608 KB |
Output is correct |
22 |
Correct |
4 ms |
11608 KB |
Output is correct |
23 |
Correct |
5 ms |
11640 KB |
Output is correct |
24 |
Correct |
4 ms |
11608 KB |
Output is correct |
25 |
Correct |
129 ms |
88760 KB |
Output is correct |
26 |
Correct |
127 ms |
88400 KB |
Output is correct |
27 |
Correct |
135 ms |
88656 KB |
Output is correct |
28 |
Correct |
153 ms |
85584 KB |
Output is correct |
29 |
Correct |
154 ms |
87348 KB |
Output is correct |
30 |
Correct |
155 ms |
86640 KB |
Output is correct |
31 |
Correct |
177 ms |
87352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
169 ms |
77476 KB |
Output is correct |
4 |
Correct |
157 ms |
77648 KB |
Output is correct |
5 |
Correct |
163 ms |
77504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9048 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
1 ms |
9048 KB |
Output is correct |
8 |
Correct |
3 ms |
9196 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9048 KB |
Output is correct |
11 |
Correct |
2 ms |
9048 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9048 KB |
Output is correct |
14 |
Correct |
2 ms |
9048 KB |
Output is correct |
15 |
Correct |
2 ms |
9048 KB |
Output is correct |
16 |
Correct |
2 ms |
9052 KB |
Output is correct |
17 |
Correct |
2 ms |
9048 KB |
Output is correct |
18 |
Correct |
4 ms |
11724 KB |
Output is correct |
19 |
Correct |
4 ms |
11612 KB |
Output is correct |
20 |
Correct |
4 ms |
11608 KB |
Output is correct |
21 |
Correct |
4 ms |
11608 KB |
Output is correct |
22 |
Correct |
4 ms |
11608 KB |
Output is correct |
23 |
Correct |
5 ms |
11640 KB |
Output is correct |
24 |
Correct |
4 ms |
11608 KB |
Output is correct |
25 |
Correct |
2 ms |
9048 KB |
Output is correct |
26 |
Correct |
4 ms |
11612 KB |
Output is correct |
27 |
Correct |
4 ms |
11608 KB |
Output is correct |
28 |
Correct |
4 ms |
11608 KB |
Output is correct |
29 |
Correct |
4 ms |
11608 KB |
Output is correct |
30 |
Correct |
4 ms |
11356 KB |
Output is correct |
31 |
Correct |
4 ms |
11356 KB |
Output is correct |
32 |
Correct |
4 ms |
11356 KB |
Output is correct |
33 |
Correct |
4 ms |
11352 KB |
Output is correct |
34 |
Correct |
129 ms |
88760 KB |
Output is correct |
35 |
Correct |
127 ms |
88400 KB |
Output is correct |
36 |
Correct |
135 ms |
88656 KB |
Output is correct |
37 |
Correct |
153 ms |
85584 KB |
Output is correct |
38 |
Correct |
154 ms |
87348 KB |
Output is correct |
39 |
Correct |
155 ms |
86640 KB |
Output is correct |
40 |
Correct |
177 ms |
87352 KB |
Output is correct |
41 |
Correct |
2 ms |
9048 KB |
Output is correct |
42 |
Correct |
2 ms |
9052 KB |
Output is correct |
43 |
Correct |
169 ms |
77476 KB |
Output is correct |
44 |
Correct |
157 ms |
77648 KB |
Output is correct |
45 |
Correct |
163 ms |
77504 KB |
Output is correct |
46 |
Correct |
144 ms |
85328 KB |
Output is correct |
47 |
Correct |
152 ms |
85328 KB |
Output is correct |
48 |
Correct |
142 ms |
86740 KB |
Output is correct |
49 |
Correct |
161 ms |
85272 KB |
Output is correct |
50 |
Correct |
182 ms |
77392 KB |
Output is correct |
51 |
Correct |
212 ms |
77288 KB |
Output is correct |
52 |
Correct |
170 ms |
77280 KB |
Output is correct |
53 |
Correct |
178 ms |
77232 KB |
Output is correct |