Cod sursa(job #2865422)

Utilizator LiviuInfoPrioteasa Liviu LiviuInfo Data 8 martie 2022 20:03:16
Problema Cerere Scor 0
Compilator cpp-64 Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.97 kb
#include <fstream>
#include <climits>
#include <vector>
#define DIM 100000
using namespace std;

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

int n, x, y;
int K[DIM], Niv[DIM], vis[DIM], G[DIM];
vector <int> L[DIM];

void dfs(int node, int niv)
{
    vis[node] = 1;
    Niv[niv] = node;
    
    int pas = 0;
    int aux = niv;
    while (K[Niv[aux]] != 0)
    {
        aux -= K[Niv[aux]];
        pas++;
    }
    G[node] = pas;

    for (int i = 0; i < L[node].size(); i++)
    {
        int child = L[node][i];
        if (vis[child] == 0)
        {
            dfs(child, niv + 1);
        }
    }
}

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> K[i];
    for (int i = 1; i < n; i++)
    {
        fin >> x >> y;
        L[x].push_back(y);
        L[y].push_back(x);
    }

    dfs(1, 1);

    

    for (int i = 1; i <= n; i++)
    {
        fout << G[i] << " ";
    }
    return 0;
}