Cod sursa(job #1048807)

Utilizator andreivFMI - vacaroiu andrei andreiv Data 6 decembrie 2013 14:18:43
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <cstdio>
#include <vector>
#define N 100009
using namespace std;

int up[N], coad[N];
vector <int> v[N];

int main()
{
    int n, i, j, s, poz;

    freopen("cerere.in", "r", stdin);
    freopen("cerere.out", "w", stdout);
    scanf("%d", &n); s = n * (n + 1) / 2;
    for (i = 1; i <= n; ++i)
        scanf("%d", &up[i]);
    for (i = 1; i < n; ++i)
    {
        int x, y;
        scanf("%d %d", &x, &y);
        v[x].push_back(y);
        s -= y;
    }
    coad[1] = s;
    poz = 1;
    while (poz)
    {
        while (!v[coad[poz]].empty())
        {
            int x = v[coad[poz]].back(); v[coad[poz]].pop_back();
            coad[++poz] = x;
            up[x] = (up[x] == 0) ? 0 : up[coad[poz - up[x]]] + 1;
            break;
        }
        if (v[coad[poz]].empty())
            --poz;
    }
for (i =1; i <= n; ++i)
    printf("%d ", up[i]);
return 0;
}