Pagini recente » Cod sursa (job #2715388) | Cod sursa (job #548551) | Cod sursa (job #1223592) | Cod sursa (job #292907) | Cod sursa (job #1643019)
#include<set>
#include<cstdio>
#include<vector>
using namespace std;
int N, maxi, sol[200009], val[200009], need[200009], type[200009], dp[200009], curr_son[200009], ap[200009], curr_bst[200009];
set < pair < int , int > > S;
vector < int > v[200009];
vector < pair < int , int > > fii[200009];
void dfs (int nod, int tata)
{
set < pair < int , int > > :: iterator it = S.lower_bound (make_pair (val[nod], 0));
if (it == S.end ()) need[nod] = 0;
else need[nod] = it->second, type[nod] = curr_son[need[nod]];
S.insert (make_pair (val[nod], nod));
for (vector < int > :: iterator it = v[nod].begin (); it != v[nod].end (); it ++)
if (*it != tata)
curr_son[nod] = *it, dfs (*it, nod);
if (sol[nod] > maxi) maxi = sol[nod];
S.erase (make_pair (val[nod], nod));
}
void solve (int nod)
{
ap[nod] = 1;
for (vector < pair < int , int > > :: iterator it = fii[nod].begin (); it != fii[nod].end (); it ++)
if (ap[it->first] == 0) solve (it->first);
for (vector < pair < int , int > > :: iterator it = fii[nod].begin (); it != fii[nod].end (); it ++)
curr_bst[it->second] = 0;
dp[nod] = 1;
for (vector < pair < int , int > > :: iterator it = fii[nod].begin (); it != fii[nod].end (); it ++)
if (dp[it->first] > curr_bst[it->second])
dp[nod] += dp[it->first] - curr_bst[it->second], curr_bst[it->second] = dp[it->first];
}
int main ()
{
freopen ("guvern.in", "r", stdin);
freopen ("guvern.out", "w", stdout);
scanf ("%d", &N);
for (int i=1; i<N; i++)
{
int x, y;
scanf ("%d %d", &x, &y);
v[x].push_back (y);
v[y].push_back (x);
}
for (int i=1; i<=N; i++)
scanf ("%d", &val[i]);
dfs (1, -1);
for (int i=1; i<=N; i++)
if (need[i] != 0) fii[need[i]].push_back (make_pair (i, type[i]));
for (int i=1; i<=N; i++)
if (need[i] == 0)
solve (i);
for (int i=1; i<=N; i++)
if (dp[i] > maxi)
maxi = dp[i];
printf ("%d\n", maxi);
return 0;
}