Cod sursa(job #2916325)

Utilizator Iordache_CezarIordache Cezar Iordache_Cezar Data 29 iulie 2022 10:41:45
Problema Cerere Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <bits/stdc++.h>
#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;
    for (auto vf : G[nod])
    {
        if (c[vf])
        {
            int t = lvl[1+nivel - c[vf]];
            dp[vf] = 1 + dp[t];
        }
        DFS(vf, nivel+1);
    }
}