This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int Nmax=3e5;
int a[Nmax];
int nodes[Nmax],pos[Nmax],p[Nmax];
pair<int,int> maxi[Nmax];
int dp[Nmax];
int nodedepth[Nmax];
pair<int,int> rmq[35][2*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 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 lg(int x)
{
return 31-__builtin_clz(x);
}
int query(int l,int r)
{
int len=lg(r-l+1);
return min(rmq[len][l],rmq[len][r-(1<<len)+1]).second;
}
int lca(int u,int v)
{
if(pos[u]>pos[v]) swap(u,v);
return query(pos[u],pos[v]);
}
void calclca()
{
dfs(1,0,0);
int k=lg(euler.size());
for(int j=0;j<euler.size();j++)
{
rmq[0][j]={depths[j],euler[j]};
}
for(int i=1;i<=k;i++)
{
for(int j=0;j+(1<<i)<euler.size();j++)
{
rmq[i][j]=min(rmq[i-1][j],rmq[i-1][j+(1<<(i-1))]);
}
}
}
int 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);
p[sefy]=sefx;
maxi[sefx]=max(maxi[sefx],maxi[sefy]);
return;
}
void activate(int nod)
{
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;
for(int i=1;i<=n;i++)
{
cin>>a[i];
nodes[i]=i;
}
sort(nodes+1,nodes+n+1,cmp);
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 (stderr)
Main.cpp: In function 'void calclca()':
Main.cpp:59:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int j=0;j<euler.size();j++)
| ~^~~~~~~~~~~~~
Main.cpp:65:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int j=0;j+(1<<i)<euler.size();j++)
| ~~~~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |