Cod sursa(job #2710154)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 22 februarie 2021 00:10:43
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cerere.in");
ofstream fout("cerere.out");

const int nmax = 100005;
int n, k[nmax], radacina[nmax], cnt[nmax], fr[nmax];
vector <int> G[nmax];

void dfs(int nod, int nivel){
    if (k[nod] != 0){
        cnt[nod] = 1 + cnt[fr[nivel - k[nod]]];
    }
    fr[nivel] = nod;
    for (auto it : G[nod]){
        dfs(it, nivel + 1);
    }
}

int main(){
    fin >> n;
    for (int i = 1; i <= n; ++i){
        fin >> k[i];
    }
    for (int i = 1; i < n; ++i){
        int x, y;
        fin >> x >> y;
        radacina[y] = 1;
        G[x].push_back(y);
    }
    int root;
    for (int i = 1; i <= n; ++i){
        if (radacina[i] == 0){
            root = i;
            break;
        }
    }
    dfs(root, 0);
    for (int i = 1; i <= n; ++i){
        fout << cnt[i] << " ";
    }
    return 0;
}