#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define INF 2023456789
#define all(x) x.begin(),x.end()
typedef pair<int,int> ii;
typedef long long ll;
#define maxn 200005
int n,a[maxn],d[maxn],pos[maxn],par[maxn],rnk[maxn],mx[maxn],p[20][maxn];
ll dp[maxn];
vector<int> AL[maxn];
int fp(int i){return par[i]==i?i:par[i]=fp(par[i]);}
void join(int x,int y){
x=fp(x),y=fp(y);
if(x==y)return;
if(rnk[x]<rnk[y])par[x]=y;
else par[y]=x;
if(rnk[x]==rnk[y])++rnk[x];
mx[par[x]]=max(mx[x],mx[y]);
}
void dfs(int u){
for(int k=1;k<20;++k){
if(p[k-1][u]==-1)p[k][u]=-1;
else p[k][u]=p[k-1][p[k-1][u]];
}
for(int v:AL[u]){
if(v==p[0][u])continue;
p[0][v]=u;
d[v]=d[u]+1;
dfs(v);
}
}
int lca(int a,int b){
if(d[a]<d[b])swap(a,b);
for(int k=19;k>=0;--k){
if(p[k][a]!=-1&&d[p[k][a]]>=d[b])a=p[k][a];
}
if(a==b)return a;
for(int k=19;k>=0;--k){
if(p[k][a]!=p[k][b])a=p[k][a],b=p[k][b];
}
return p[0][a];
}
inline int dist(int a,int b){
return d[a]+d[b]-2*d[lca(a,b)];
}
int main(){
sf("%d",&n);
for(int i=1;i<=n;++i){
sf("%d",&a[i]);
pos[a[i]]=i;
par[i]=i,mx[i]=a[i];
}
for(int i=0;i<n-1;++i){
int a,b;sf("%d%d",&a,&b);
AL[a].pb(b);AL[b].pb(a);
}
p[0][1]=-1;
dfs(1);
for(int i=2;i<=n;++i){
int u=pos[i];
for(int v:AL[u]){
if(a[v]>i)continue;
int m=mx[fp(v)];
int w=pos[m];
dp[i]=max(dp[i],dp[m]+dist(u,w));
join(u,v);
}
}
pf("%lld\n",dp[n]);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:60:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | sf("%d",&n);
| ^
Main.cpp:62:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | sf("%d",&a[i]);
| ^
Main.cpp:67:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | int a,b;sf("%d%d",&a,&b);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5136 KB |
Output is correct |
5 |
Correct |
3 ms |
5132 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5136 KB |
Output is correct |
5 |
Correct |
3 ms |
5132 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5136 KB |
Output is correct |
12 |
Correct |
3 ms |
5136 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5140 KB |
Output is correct |
16 |
Correct |
3 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5136 KB |
Output is correct |
5 |
Correct |
3 ms |
5132 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5136 KB |
Output is correct |
12 |
Correct |
3 ms |
5136 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5140 KB |
Output is correct |
16 |
Correct |
3 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6100 KB |
Output is correct |
19 |
Correct |
6 ms |
6100 KB |
Output is correct |
20 |
Correct |
6 ms |
6172 KB |
Output is correct |
21 |
Correct |
6 ms |
6228 KB |
Output is correct |
22 |
Correct |
7 ms |
6228 KB |
Output is correct |
23 |
Correct |
6 ms |
6164 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5136 KB |
Output is correct |
5 |
Correct |
3 ms |
5132 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5136 KB |
Output is correct |
12 |
Correct |
3 ms |
5136 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5140 KB |
Output is correct |
16 |
Correct |
3 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6100 KB |
Output is correct |
19 |
Correct |
6 ms |
6100 KB |
Output is correct |
20 |
Correct |
6 ms |
6172 KB |
Output is correct |
21 |
Correct |
6 ms |
6228 KB |
Output is correct |
22 |
Correct |
7 ms |
6228 KB |
Output is correct |
23 |
Correct |
6 ms |
6164 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
7 ms |
6024 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
6 ms |
5972 KB |
Output is correct |
29 |
Correct |
7 ms |
6100 KB |
Output is correct |
30 |
Correct |
6 ms |
5844 KB |
Output is correct |
31 |
Correct |
6 ms |
5936 KB |
Output is correct |
32 |
Correct |
6 ms |
5912 KB |
Output is correct |
33 |
Correct |
6 ms |
5844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5136 KB |
Output is correct |
5 |
Correct |
3 ms |
5132 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5136 KB |
Output is correct |
12 |
Correct |
3 ms |
5136 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5140 KB |
Output is correct |
16 |
Correct |
3 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6100 KB |
Output is correct |
19 |
Correct |
6 ms |
6100 KB |
Output is correct |
20 |
Correct |
6 ms |
6172 KB |
Output is correct |
21 |
Correct |
6 ms |
6228 KB |
Output is correct |
22 |
Correct |
7 ms |
6228 KB |
Output is correct |
23 |
Correct |
6 ms |
6164 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
125 ms |
48088 KB |
Output is correct |
26 |
Correct |
146 ms |
46688 KB |
Output is correct |
27 |
Correct |
148 ms |
47380 KB |
Output is correct |
28 |
Correct |
362 ms |
45900 KB |
Output is correct |
29 |
Correct |
395 ms |
45916 KB |
Output is correct |
30 |
Correct |
397 ms |
46988 KB |
Output is correct |
31 |
Correct |
398 ms |
46000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
299 ms |
35712 KB |
Output is correct |
4 |
Correct |
252 ms |
33312 KB |
Output is correct |
5 |
Correct |
248 ms |
34144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 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 |
3 ms |
5136 KB |
Output is correct |
5 |
Correct |
3 ms |
5132 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5136 KB |
Output is correct |
12 |
Correct |
3 ms |
5136 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
3 ms |
5204 KB |
Output is correct |
15 |
Correct |
3 ms |
5140 KB |
Output is correct |
16 |
Correct |
3 ms |
5100 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
6 ms |
6100 KB |
Output is correct |
19 |
Correct |
6 ms |
6100 KB |
Output is correct |
20 |
Correct |
6 ms |
6172 KB |
Output is correct |
21 |
Correct |
6 ms |
6228 KB |
Output is correct |
22 |
Correct |
7 ms |
6228 KB |
Output is correct |
23 |
Correct |
6 ms |
6164 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
7 ms |
6024 KB |
Output is correct |
27 |
Correct |
6 ms |
6100 KB |
Output is correct |
28 |
Correct |
6 ms |
5972 KB |
Output is correct |
29 |
Correct |
7 ms |
6100 KB |
Output is correct |
30 |
Correct |
6 ms |
5844 KB |
Output is correct |
31 |
Correct |
6 ms |
5936 KB |
Output is correct |
32 |
Correct |
6 ms |
5912 KB |
Output is correct |
33 |
Correct |
6 ms |
5844 KB |
Output is correct |
34 |
Correct |
125 ms |
48088 KB |
Output is correct |
35 |
Correct |
146 ms |
46688 KB |
Output is correct |
36 |
Correct |
148 ms |
47380 KB |
Output is correct |
37 |
Correct |
362 ms |
45900 KB |
Output is correct |
38 |
Correct |
395 ms |
45916 KB |
Output is correct |
39 |
Correct |
397 ms |
46988 KB |
Output is correct |
40 |
Correct |
398 ms |
46000 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
299 ms |
35712 KB |
Output is correct |
44 |
Correct |
252 ms |
33312 KB |
Output is correct |
45 |
Correct |
248 ms |
34144 KB |
Output is correct |
46 |
Correct |
573 ms |
40736 KB |
Output is correct |
47 |
Correct |
555 ms |
39164 KB |
Output is correct |
48 |
Correct |
515 ms |
41360 KB |
Output is correct |
49 |
Correct |
567 ms |
39584 KB |
Output is correct |
50 |
Correct |
455 ms |
35608 KB |
Output is correct |
51 |
Correct |
462 ms |
35872 KB |
Output is correct |
52 |
Correct |
418 ms |
35904 KB |
Output is correct |
53 |
Correct |
422 ms |
33328 KB |
Output is correct |