Pagini recente » Cod sursa (job #2708735) | Cod sursa (job #2400335) | Cod sursa (job #292347) | Cod sursa (job #2879314) | Cod sursa (job #3173260)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("guvern.in");
ofstream fout("guvern.out");
int n;
vector <int> g[200005];
int a[200005];
int depth[200005];
int p[20][200005];
set <pair <int,int>> s;
int b[200005];
int bb[200005];
int c[200005];
queue <int> q;
void dfs(int u,int pp)
{
auto it=s.lower_bound(make_pair(a[u],u));
if(it!=s.end())
{
b[u]=it->second;
bb[u]=u;
for(int i=0; (1<<i)<=depth[u]-depth[b[u]]-1; i++)
if((depth[u]-depth[b[u]]-1)&(1<<i))
bb[u]=p[i][bb[u]];
}
else
b[u]=bb[u]=n+1;
s.insert(make_pair(a[u],u));
for(int v:g[u])
{
if(v!=pp)
{
c[u]++;
depth[v]=depth[u]+1;
p[0][v]=u;
for(int j=1; (1<<j)<=depth[v]; j++)
p[j][v]=p[j-1][p[j-1][v]];
dfs(v,u);
}
}
s.erase(make_pair(a[u],u));
}
int aux1[200005];
int aux2[200005];
int dp[200005];
int main()
{
fin>>n;
for(int i=1; i<n; i++)
{
int x,y;
fin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i=1; i<=n; i++)
fin>>a[i];
dfs(1,0);
for(int i=1; i<=n; i++)
if(c[i]==0)
q.push(i);
int ans=0;
while(!q.empty())
{
int x=q.front();
q.pop();
int sum=0;
dp[x]=1;
for(int y:g[x])
{
if(y!=p[0][x])
{
dp[x]+=aux1[y];
sum+=aux2[y];
aux2[x]=max(aux2[x],aux2[y]);
}
}
aux2[x]=max(aux2[x],dp[x]);
ans=max(ans,sum);
ans=max(ans,dp[x]);
if(bb[x]<=n)
aux1[bb[x]]=max(aux1[bb[x]],dp[x]);
c[p[0][x]]--;
if(c[p[0][x]]==0)
q.push(p[0][x]);
}
fout<<ans<<"\n";
return 0;
}