Cod sursa(job #1538703)

Utilizator FairPlay94George Cioroiu FairPlay94 Data 29 noiembrie 2015 17:20:29
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <cstdio>
#include <iostream>
#include <set>
#include <climits>
#include <map>
#include <algorithm>
#include <list>
#include <vector>
#include <utility>
#include <cstdlib>

using namespace std;

int tata[100005], rez[100005], v[100005], stramos[100005], parc[100005];
list<int> E[100005];
bool viz[100005];

void dfs(int nod, int nivel) {
    if (v[nod] != 0) {
        rez[nod] = rez[parc[nivel - v[nod]]] + 1;
    } else {
        rez[nod] = 0;
    }
    parc[nivel] = nod;
    for (list<int>::iterator it = E[nod].begin(); it != E[nod].end(); it++) {
        dfs(*it, nivel + 1);
    }
}

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

    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> v[i];
    }
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        tata[b] = a;
        E[a].push_back(b);
    }
    int root = 0;
    for (int i = 1; i <= n; i++) {
        if (tata[i] == 0) {
            root = i;
            break;
        }
    }
    dfs(root, 0);
    for (int i = 1; i <= n; i++) {
        cout << rez[i] << " ";
    }

    return 0;
}