#include<stdio.h>
#define N 200001
int n,p[N],tin[N],tout[N],aux[N],paux[N],dd[N],pp[N],jj[N];
struct { int to,prev; } e[N<<1]; int ee,h[N];
int timer=1;
void dfs(int u,int p)
{
aux[tin[u]=timer++]=u;
for(int j=h[u];j;j=e[j].prev)if(e[j].to-p)
{
int v=e[j].to;
pp[v]=u;
jj[v]=(dd[u]-dd[jj[u]]==dd[jj[u]]-dd[jj[jj[u]]])?jj[jj[u]]:u;
dd[v]=dd[u]+1;
dfs(e[j].to,u);
}
tout[u]=timer-1;
}
long long min_(long long a,long long b){return a<b?a:b;}
long long max_(long long a,long long b){return a>b?a:b;}
long long t[N<<2],lz[N<<2];
void build(int v,int l,int r)
{
if(l==r){t[v]=p[aux[l]];return;}
build(v<<1,l,(l+r)/2);
build(v<<1|1,(l+r)/2+1,r);
t[v]=max_(t[v<<1],t[v<<1|1]);
}
void upd(int v,int l,int r,int x,int y,int k)
{
if(r<x||y<l)return;
if(x<=l&&r<=y) { lz[v]+=k; return; }
upd(v<<1,l,(l+r)/2,x,y,k);
upd(v<<1|1,(l+r)/2+1,r,x,y,k);
t[v]=max_(t[v<<1]+lz[v<<1],t[v<<1|1]+lz[v<<1|1]);
}
long long get(){return t[1]+lz[1];}
int lca(int u,int v)
{
if(dd[u]>dd[v])return lca(v,u);
while(dd[v]>dd[u])
v=dd[jj[v]]>=dd[u]?jj[v]:pp[v];
while(u-v)
if(jj[v]!=jj[u])u=jj[u],v=jj[v];
else u=pp[u],v=pp[v];
return u;
}
int dist(int u,int v){return dd[u]+dd[v]-2*dd[lca(u,v)];}
long long dfs1(int u)
{
long long ans=0,next;
upd(1,1,n,tin[u],tout[u],-1e9);
next=get();
if(next>=1)ans=max_(ans,dfs1(paux[next])+dist(paux[next],u));
upd(1,1,n,1,tin[u]-1,-1e9);
upd(1,1,n,tout[u]+1,n,-1e9);
for(int j=h[u];j;j=e[j].prev)
{
int v=e[j].to;
if(v!=pp[u])
{
upd(1,1,n,tin[v],tout[v],1e9);
next=get();
if(next>=1)ans=max_(ans,dfs1(paux[next])+dist(paux[next],u));
upd(1,1,n,tin[v],tout[v],-1e9);
}
}
upd(1,1,n,1,n,1e9);
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d",p+i),paux[p[i]]=i;
for(int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),e[++ee].to=v,e[ee].prev=h[u],h[u]=ee,e[++ee].to=u,e[ee].prev=h[v],h[v]=ee;
pp[1]=jj[1]=1;
dfs(1,1);
build(1,1,n);
printf("%lld\n",dfs1(paux[n]));
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:86:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d",&n);
| ^~~~~~~~~~~~~~
Main.c:87:26: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | for(int i=1;i<=n;++i)scanf("%d",p+i),paux[p[i]]=i;
| ^~~~~~~~~~~~~~~
Main.c:88:29: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | for(int i=1,u,v;i<n;++i)scanf("%d%d",&u,&v),e[++ee].to=v,e[ee].prev=h[u],h[u]=ee,e[++ee].to=u,e[ee].prev=h[v],h[v]=ee;
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8536 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8536 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
5 ms |
9308 KB |
Output is correct |
19 |
Correct |
5 ms |
9308 KB |
Output is correct |
20 |
Correct |
7 ms |
9304 KB |
Output is correct |
21 |
Correct |
5 ms |
9052 KB |
Output is correct |
22 |
Correct |
5 ms |
9052 KB |
Output is correct |
23 |
Correct |
5 ms |
9052 KB |
Output is correct |
24 |
Correct |
7 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8536 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
5 ms |
9308 KB |
Output is correct |
19 |
Correct |
5 ms |
9308 KB |
Output is correct |
20 |
Correct |
7 ms |
9304 KB |
Output is correct |
21 |
Correct |
5 ms |
9052 KB |
Output is correct |
22 |
Correct |
5 ms |
9052 KB |
Output is correct |
23 |
Correct |
5 ms |
9052 KB |
Output is correct |
24 |
Correct |
7 ms |
9052 KB |
Output is correct |
25 |
Correct |
1 ms |
8540 KB |
Output is correct |
26 |
Correct |
6 ms |
9212 KB |
Output is correct |
27 |
Correct |
5 ms |
9048 KB |
Output is correct |
28 |
Correct |
6 ms |
9048 KB |
Output is correct |
29 |
Correct |
5 ms |
9304 KB |
Output is correct |
30 |
Correct |
6 ms |
8844 KB |
Output is correct |
31 |
Correct |
8 ms |
8792 KB |
Output is correct |
32 |
Correct |
6 ms |
8796 KB |
Output is correct |
33 |
Correct |
5 ms |
8904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8536 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
5 ms |
9308 KB |
Output is correct |
19 |
Correct |
5 ms |
9308 KB |
Output is correct |
20 |
Correct |
7 ms |
9304 KB |
Output is correct |
21 |
Correct |
5 ms |
9052 KB |
Output is correct |
22 |
Correct |
5 ms |
9052 KB |
Output is correct |
23 |
Correct |
5 ms |
9052 KB |
Output is correct |
24 |
Correct |
7 ms |
9052 KB |
Output is correct |
25 |
Correct |
209 ms |
40732 KB |
Output is correct |
26 |
Correct |
206 ms |
42828 KB |
Output is correct |
27 |
Correct |
198 ms |
43092 KB |
Output is correct |
28 |
Correct |
177 ms |
41280 KB |
Output is correct |
29 |
Correct |
198 ms |
41296 KB |
Output is correct |
30 |
Correct |
178 ms |
41376 KB |
Output is correct |
31 |
Correct |
205 ms |
41412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
227 ms |
23400 KB |
Output is correct |
4 |
Correct |
229 ms |
27240 KB |
Output is correct |
5 |
Correct |
247 ms |
27000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8536 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
1 ms |
8540 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
8540 KB |
Output is correct |
18 |
Correct |
5 ms |
9308 KB |
Output is correct |
19 |
Correct |
5 ms |
9308 KB |
Output is correct |
20 |
Correct |
7 ms |
9304 KB |
Output is correct |
21 |
Correct |
5 ms |
9052 KB |
Output is correct |
22 |
Correct |
5 ms |
9052 KB |
Output is correct |
23 |
Correct |
5 ms |
9052 KB |
Output is correct |
24 |
Correct |
7 ms |
9052 KB |
Output is correct |
25 |
Correct |
1 ms |
8540 KB |
Output is correct |
26 |
Correct |
6 ms |
9212 KB |
Output is correct |
27 |
Correct |
5 ms |
9048 KB |
Output is correct |
28 |
Correct |
6 ms |
9048 KB |
Output is correct |
29 |
Correct |
5 ms |
9304 KB |
Output is correct |
30 |
Correct |
6 ms |
8844 KB |
Output is correct |
31 |
Correct |
8 ms |
8792 KB |
Output is correct |
32 |
Correct |
6 ms |
8796 KB |
Output is correct |
33 |
Correct |
5 ms |
8904 KB |
Output is correct |
34 |
Correct |
209 ms |
40732 KB |
Output is correct |
35 |
Correct |
206 ms |
42828 KB |
Output is correct |
36 |
Correct |
198 ms |
43092 KB |
Output is correct |
37 |
Correct |
177 ms |
41280 KB |
Output is correct |
38 |
Correct |
198 ms |
41296 KB |
Output is correct |
39 |
Correct |
178 ms |
41376 KB |
Output is correct |
40 |
Correct |
205 ms |
41412 KB |
Output is correct |
41 |
Correct |
1 ms |
8540 KB |
Output is correct |
42 |
Correct |
1 ms |
8540 KB |
Output is correct |
43 |
Correct |
227 ms |
23400 KB |
Output is correct |
44 |
Correct |
229 ms |
27240 KB |
Output is correct |
45 |
Correct |
247 ms |
27000 KB |
Output is correct |
46 |
Correct |
305 ms |
38808 KB |
Output is correct |
47 |
Correct |
264 ms |
38996 KB |
Output is correct |
48 |
Correct |
280 ms |
38700 KB |
Output is correct |
49 |
Correct |
287 ms |
38740 KB |
Output is correct |
50 |
Correct |
248 ms |
25944 KB |
Output is correct |
51 |
Correct |
265 ms |
26192 KB |
Output is correct |
52 |
Correct |
243 ms |
25812 KB |
Output is correct |
53 |
Correct |
266 ms |
25880 KB |
Output is correct |