Cod sursa(job #3158047)

Utilizator TonyyAntonie Danoiu Tonyy Data 17 octombrie 2023 17:12:01
Problema Cerere Scor 0
Compilator cpp-64 Status done
Runda HLO 2023 - Cls 11-12 - Tema 0 Marime 1.02 kb
#include <fstream>
#include <iostream>
using namespace std;

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

int n, k[100000], v[100000], a, b;

void citire()
{
    fin >> n;
    for (int i = 1 ; i <= n ; ++i)
        fin >> k[i];
    for (int i = 1 ; i < n ; ++i)
    {
        fin >> a >> b;
        v[b] = a;
    }
}

int main()
{
    citire();
    for (int i = 1 ; i <= n ; ++i)
    {
        if (!k[i])
            fout << 0 << " ";
        else
        {
            bool gasit = false;
            int c = 0;
            int x = v[i];
            int y = k[i];
            while (!gasit)
            {
                for (int j = 1 ; j < y ; ++j)
                    x = v[x];
                ++c;
                if (!k[x])
                    gasit = true;
                else
                {
                    x = v[x];
                    y = k[x];
                }
                    
            }
            fout << c << " ";
        }
    }
    return 0;
}