Cod sursa(job #2348077)

Utilizator lupulescu2001Lupulescu Vlad lupulescu2001 Data 19 februarie 2019 12:37:47
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int nMax = 100000;
int st[nMax + 5], k[nMax + 5], tt[nMax + 5], dp[nMax + 5];
int n, rad;
vector<int> g[nMax + 5];

void DFS(int nod) {
    st[++st[0]] = nod;
    if (k[nod])
        dp[nod] = dp[st[st[0] - k[nod]]] + 1;
    for (auto i : g[nod])
        if (i != tt[nod])
            DFS(i);
    st[0]--;
}

void Solve() {
    for (int i = 1; i <= n; i++)
        if (tt[i] == 0) {
            rad = i;
            break;
        }
    DFS(rad);
}

void Print() {
    for (int i = 1; i <= n; i++)
        fout << dp[i] << " ";
    fout << '\n';
}

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;
        g[x].push_back(y);
        g[y].push_back(x);
        tt[y] = x;
    }
    Solve();
    Print();
    return 0;
}