Cod sursa(job #586578)

Utilizator antoanelaAntoanela Siminiuc antoanela Data 2 mai 2011 14:16:50
Problema Guvern Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <cstdio>
#include <vector>
using namespace std;
#define nmax 200010

int n, u[nmax], t[nmax], c[nmax], a[nmax], d[nmax], sol;
vector <int> g[nmax];

void df(int nod)
{
    int i, v;
    for (i=0; i<g[nod].size(); i++)
    {
        v=g[nod][i];
        if (!u[v])
        {
            d[nod]=1;
            t[v]=nod;
            u[v]=1;
            df(v);
        }
    }
}

void calc(int x)
{
    int r, nod=t[x];
    r=((1<<30)-1)*2+1;
    while (nod)
    {
        if (c[nod]>c[x] && c[nod]<r)
        {
            r=c[nod];
            a[x]=nod;
        }
        nod=t[nod];
    }
}

void nr(int x)
{
    int c=0, nod=x;
    while (nod)
    {
        c++;
        u[nod]=1;
        nod=a[nod];
    }
    if (c>sol) sol=c;
}

int main()
{
    freopen("guvern.in","r",stdin);
    freopen("guvern.out","w",stdout);
    scanf("%d",&n);
    int i, x, y;
    for (i=1; i<n; i++)
    {

        scanf("%d %d", &x, &y);
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (i=1; i<=n; i++) scanf("%d", &c[i]);
    u[1]=1;
    df(1);
    for (i=1; i<=n; i++) u[i]=0;
    for (i=1; i<=n; i++) calc(i);
    for (i=1; i<=n; i++)
      //  if (!u[i])
            nr(i);
    printf("%d\n",sol);
}