Cod sursa(job #1152993)

Utilizator taigi100Cazacu Robert taigi100 Data 25 martie 2014 10:19:39
Problema Cerere Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
/*
    Keep It Simple!
*/

#include<stdio.h>
#include<list>

#define MaxN 100001

using namespace std;

int N;
list<int> G[MaxN];
bool InLines[MaxN];
int root,k[MaxN],Dp[MaxN];
int Dads[MaxN];

void DFS(int node)
{
    int cnt = k[node];
    int aux = node;
    while(cnt && aux != root)
    {
        aux = Dads[aux];
        cnt--;
    }

    Dp[node] = Dp[aux]+1;

    for(list<int>::iterator it = G[node].begin(); it!=G[node].end(); it++)
    {
        Dads[*it] = node;
        DFS(*it);
    }
}

int main()
{
    freopen("cerere.in","r",stdin);
    freopen("cerere.out","w",stdout);

    scanf("%d",&N);
    for(int i=1;i<=N;i++)
        scanf("%d",&k[i]);

    int x,y;

    for(int i=2;i<=N;i++)
        {
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
            InLines[y] = 1;
        }
    for(int i=1;i<=N;i++)
        if(!InLines[i])
        {
            root = i;
            break;
        }

    Dp[root] = k[root];
    DFS(root);

    for(int i=1;i<=N;i++)
        printf("%d ",Dp[i]-1);
}