Cod sursa(job #1132800)

Utilizator SmarandaMaria Pandele Smaranda Data 3 martie 2014 22:09:53
Problema Cerere Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <cstdio>

using namespace std;

const long N = 100005;

long n, k [N], t [N], ans [N];

void read () {
    long i, a, b;

    scanf ("%ld", &n);
    for (i = 1; i <= n; i ++) {
        scanf ("%ld", &k [i]);
        ans [i] = -1;
    }
    for (i = 1; i < n; i ++) {
        scanf ("%ld%ld", &a, &b);
        t [b] = a;
    }
}

long dfs (long x) {
    long i, father;

    if (ans [x] != -1)
        return ans [x];
    //k [x] stramos
    if (k [x] == 0)
        return 0;

    father = x;
    for (i = 1; i <= k [x]; i ++)
        father = t [father];
    return 1 + dfs (father);
}

void solve () {
    long i;

    for (i = 1; i <= n; i ++)
        ans [i] = dfs (i);
    for (i = 1; i <= n; i ++)
        printf ("%ld ", ans [i]);
    printf ("\n");
}

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

    read ();
    solve ();
    return 0;
}