Cod sursa(job #2916644)

Utilizator Iordache_CezarIordache Cezar Iordache_Cezar Data 31 iulie 2022 10:16:19
Problema Cerere Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define NMAX 100008

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

int n, root, dp[NMAX], c[NMAX], tata[NMAX], lvl[NMAX];
vector <int> G[NMAX];

void citire();
void DFS(int nod, int nivel);

int main()
{
    citire();
    DFS(root, 0);
    for (int i = 1; i <= n; i++)
        fout << dp[i] << ' ';
    return 0;
}

void citire()
{
    int a, b;
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> c[i];
    for (int i = 1; i < n; i++)
    {
        fin >> a >> b;
        G[a].push_back(b);
        tata[b] = a;
    }

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

void DFS(int nod, int nivel)
{
    lvl[nivel] = nod;
    if (c[nod])
        dp[nod] =  1 + dp[lvl[nivel-c[nod]]];
    for (auto vf : G[nod])
        DFS(vf, nivel+1);
}