Pagini recente » Cod sursa (job #3190501) | Cod sursa (job #919435) | Cod sursa (job #2229116) | Cod sursa (job #3237061) | Cod sursa (job #586199)
Cod sursa(job #586199)
#include<fstream>
#include<vector>
#include<algorithm>
#include<bitset>
using namespace std;
ifstream fin("guvern.in");
ofstream fout("guvern.out");
int n;
int dif[200001];
int sol[200001];
vector<int > a[200001];
bool s[200001];
void read();
void solve();
void DF(int );
int main()
{
read();
solve();
return 0;
}
void read()
{
fin >> n;
int x, y;
for(int i = 1; i < n; ++i)
{
fin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int i = 1; i <= n; ++i)
fin >> dif[i];
fin.close();
}
void solve()
{
DF(1);
fout << 2 * ( *max_element(sol+1, sol+n+1) );
}
void DF(int x)
{
s[x] = 1;
if(a[x].size() == 1 && x != 1)
{
sol[x] = 1;
return;
}
for(unsigned int i = 0; i < a[x].size(); ++i)
if( !s[ a[x][i] ] )
{
int y = a[x][i];
DF( y );
if( dif[x] < dif[y])
sol[x] = max(sol[y] + 1, sol[x]);
}
}