Cod sursa(job #2494203)

Utilizator sichetpaulSichet Paul sichetpaul Data 17 noiembrie 2019 15:18:55
Problema Cerere Scor 85
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;

ifstream f("cerere.in");
ofstream g("cerere.out");

int N, nr;
int pos[Nmax], ans[Nmax], st[Nmax];
vector<int> G[Nmax];
void DFS(int node, int father) {
    ++nr, st[nr] = node;
    if (pos[node] && nr > pos[node]) ans[node] = 1 + ans[st[nr - pos[node]]];
    for (auto it: G[node])
        if (it != father) DFS(it, node);
    --nr;
}
int main()
{
    f >> N;
    for (int i = 1; i <= N; ++i)
        f >> pos[i];
    for (int i = 1; i < N; ++i) {
        int x, y;
        f >> x >> y;
        G[x].push_back(y);
    }

    for (int i = 1; i <= N; ++i)
     if (pos[i] == 0) DFS(i, 0);

    for (int i = 1; i <= N; ++i)
         g << ans[i] << " ";

    return 0;
}