Cod sursa(job #2778872)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 2 octombrie 2021 12:47:43
Problema Cerere Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>
#define DIM 100005

using namespace std;

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

int n, K[DIM], G[DIM], root, node[DIM];
bitset <DIM> v;
vector <int> edges[DIM];

void dfs(int nod, int niv)
{
    node[niv] = nod;
    G[nod] = G[node[niv - K[nod]]] + 1;
    for(auto child : edges[nod])
        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);
        v[y] = 1;
    }

    for(int i = 1; i <= n; i++)
        if(!v[i])
        {
            root = i;
            break;
        }

    dfs(root, 0);

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

    return 0;
}