#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int Nmax=2e5+5;
int a[Nmax];
int nodes[Nmax],pos[Nmax],sz[Nmax],p[Nmax];
pair<int,int> maxi[Nmax];
ll dp[Nmax];
ll nodedepth[Nmax];
pair<int,int> aint[4*Nmax];
vector<int> euler;
vector<int> depths;
vector<int> v[Nmax];
int n;
int sol=0;
int sef(int x)
{
if(p[x]==x) return x;
p[x]=sef(p[x]);
return p[x];
}
bool cmp(int x,int y)
{
return a[x]<a[y];
}
void build(int nod,int l,int r)
{
if(l==r)
{
aint[nod]={depths[l],euler[l]};
return;
}
int mid=(l+r)/2;
build(2*nod,l,mid);
build(2*nod+1,mid+1,r);
aint[nod]=min(aint[2*nod],aint[2*nod+1]);
return;
}
pair<int,int> query(int nod,int l,int r,int st,int dr)
{
if(st<=l && r<=dr) return aint[nod];
int mid=(l+r)/2;
if(dr<=mid) return query(2*nod,l,mid,st,dr);
if(mid<st) return query(2*nod+1,mid+1,r,st,dr);
return min(query(2*nod,l,mid,st,mid),query(2*nod+1,mid+1,r,mid+1,dr));
}
void dfs(int nod,int prev,int depth)
{
euler.push_back(nod);
depths.push_back(depth);
nodedepth[nod]=depth;
pos[nod]=euler.size()-1;
for(auto u:v[nod])
{
if(u!=prev)
{
dfs(u,nod,depth+1);
euler.push_back(nod);
depths.push_back(depth);
}
}
}
int lca(int u,int v)
{
if(pos[u]>pos[v]) swap(u,v);
return query(1,0,euler.size()-1,pos[u],pos[v]).second;
}
void calclca()
{
dfs(1,0,0);
build(1,0,euler.size()-1);
}
ll dist(int u,int v)
{
int nod=lca(u,v);
return nodedepth[u]+nodedepth[v]-2*nodedepth[nod];
}
void unite(int x,int y)
{
int sefx=sef(x);
int sefy=sef(y);
if(sz[sefx]<sz[sefy]) swap(sefx,sefy);
p[sefy]=sefx;
sz[sefx]+=sz[sefy];
maxi[sefx]=max(maxi[sefx],maxi[sefy]);
return;
}
void activate(int nod)
{
sz[nod]=1;
p[nod]=nod;
maxi[nod]={a[nod],nod};
for(auto u:v[nod])
{
if(p[u]!=0)
{
int nxt=maxi[sef(u)].second;
dp[nod]=max(dp[nod],dist(nod,nxt)+dp[nxt]);
unite(nod,u);
}
}
}
signed main()
{
cin>>n;
int start;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]==n) start=i;
nodes[i]=i;
}
sort(nodes+1,nodes+n+1,cmp);
bool ok=0;
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
calclca();
for(int i=1;i<=n;i++)
{
activate(nodes[i]);
}
cout<<dp[nodes[n]]<<'\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:106:9: warning: variable 'start' set but not used [-Wunused-but-set-variable]
106 | int start;
| ^~~~~
Main.cpp:114:10: warning: unused variable 'ok' [-Wunused-variable]
114 | bool ok=0;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14936 KB |
Output is correct |
17 |
Correct |
2 ms |
14936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14936 KB |
Output is correct |
17 |
Correct |
2 ms |
14936 KB |
Output is correct |
18 |
Correct |
7 ms |
15452 KB |
Output is correct |
19 |
Correct |
8 ms |
15452 KB |
Output is correct |
20 |
Correct |
9 ms |
15640 KB |
Output is correct |
21 |
Correct |
7 ms |
15452 KB |
Output is correct |
22 |
Correct |
7 ms |
15452 KB |
Output is correct |
23 |
Correct |
9 ms |
15452 KB |
Output is correct |
24 |
Correct |
8 ms |
15632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14936 KB |
Output is correct |
17 |
Correct |
2 ms |
14936 KB |
Output is correct |
18 |
Correct |
7 ms |
15452 KB |
Output is correct |
19 |
Correct |
8 ms |
15452 KB |
Output is correct |
20 |
Correct |
9 ms |
15640 KB |
Output is correct |
21 |
Correct |
7 ms |
15452 KB |
Output is correct |
22 |
Correct |
7 ms |
15452 KB |
Output is correct |
23 |
Correct |
9 ms |
15452 KB |
Output is correct |
24 |
Correct |
8 ms |
15632 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
7 ms |
15448 KB |
Output is correct |
27 |
Correct |
8 ms |
15452 KB |
Output is correct |
28 |
Correct |
8 ms |
15480 KB |
Output is correct |
29 |
Correct |
7 ms |
15452 KB |
Output is correct |
30 |
Correct |
8 ms |
15232 KB |
Output is correct |
31 |
Correct |
7 ms |
15316 KB |
Output is correct |
32 |
Correct |
9 ms |
15196 KB |
Output is correct |
33 |
Correct |
7 ms |
15196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14936 KB |
Output is correct |
17 |
Correct |
2 ms |
14936 KB |
Output is correct |
18 |
Correct |
7 ms |
15452 KB |
Output is correct |
19 |
Correct |
8 ms |
15452 KB |
Output is correct |
20 |
Correct |
9 ms |
15640 KB |
Output is correct |
21 |
Correct |
7 ms |
15452 KB |
Output is correct |
22 |
Correct |
7 ms |
15452 KB |
Output is correct |
23 |
Correct |
9 ms |
15452 KB |
Output is correct |
24 |
Correct |
8 ms |
15632 KB |
Output is correct |
25 |
Incorrect |
221 ms |
42128 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
3 ms |
14940 KB |
Output is correct |
3 |
Incorrect |
337 ms |
29608 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14940 KB |
Output is correct |
3 |
Correct |
3 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
2 ms |
14940 KB |
Output is correct |
6 |
Correct |
2 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
2 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14936 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
3 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14936 KB |
Output is correct |
13 |
Correct |
3 ms |
14940 KB |
Output is correct |
14 |
Correct |
2 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14936 KB |
Output is correct |
17 |
Correct |
2 ms |
14936 KB |
Output is correct |
18 |
Correct |
7 ms |
15452 KB |
Output is correct |
19 |
Correct |
8 ms |
15452 KB |
Output is correct |
20 |
Correct |
9 ms |
15640 KB |
Output is correct |
21 |
Correct |
7 ms |
15452 KB |
Output is correct |
22 |
Correct |
7 ms |
15452 KB |
Output is correct |
23 |
Correct |
9 ms |
15452 KB |
Output is correct |
24 |
Correct |
8 ms |
15632 KB |
Output is correct |
25 |
Correct |
3 ms |
14940 KB |
Output is correct |
26 |
Correct |
7 ms |
15448 KB |
Output is correct |
27 |
Correct |
8 ms |
15452 KB |
Output is correct |
28 |
Correct |
8 ms |
15480 KB |
Output is correct |
29 |
Correct |
7 ms |
15452 KB |
Output is correct |
30 |
Correct |
8 ms |
15232 KB |
Output is correct |
31 |
Correct |
7 ms |
15316 KB |
Output is correct |
32 |
Correct |
9 ms |
15196 KB |
Output is correct |
33 |
Correct |
7 ms |
15196 KB |
Output is correct |
34 |
Incorrect |
221 ms |
42128 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |