Cod sursa(job #2834583)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 17 ianuarie 2022 11:42:16
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>
#define DIM 100005

using namespace std;

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

int n, root, k[DIM], path[DIM], ans[DIM];
bitset <DIM> v, eFiu;
vector <int> edges[DIM];


void dfs(int node, int niv)
{
    v[node] = 1;
    path[niv] = node;
    ans[node] = ans[path[niv - k[node]]] + 1;

    for (auto child: edges[node])
        if (!v[child])
            dfs(child, niv + 1);
}

int main()
{
    f >> n;
    for (int i = 1; i <= n; i++)
        f >> k[i];

    for (int i = 1; i < n; i++)
    {
        int x, y;
        f >> x >> y;
        edges[x].push_back(y);
        eFiu[y] = 1;
    }

    for (int i = 1; i <= n; i++)
        if (!eFiu[i])
            root = i;

    dfs(root, 1);

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

    return 0;
}