Cod sursa(job #2922410)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 8 septembrie 2022 10:53:24
Problema Cerere Scor 85
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <vector>

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

const int maxN = 100005;

int n, s[maxN], ans[maxN], lvl[maxN];
bool used[maxN];
vector <int> G[maxN];

void dfs(int nod, int depth)
{
    lvl[depth] = nod;
    if(s[nod] == 0)
        ans[nod] = 0;
    else
        ans[nod] = ans[lvl[depth - s[nod]]] + 1;
    for(int vecin : G[nod])
        dfs(vecin, depth + 1);
}

int main()
{
    fin >> n;
    for(int i = 1; i <= n; i++)
        fin >> s[i];
    for(int i = 1; i < n; i++)
    {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
        used[y] = 1;
    }
    int root = -1;
    for(int i = 1; i <= n && root == -1; i++)
        if(!used[i])
            root = i;
    dfs(root, 0);
    for(int i = 1; i <= n; i++)
        fout << ans[i] << ' ';
    return 0;
}