Cod sursa(job #1899537)

Utilizator PaulStighiStiegelbauer Paul-Alexandru PaulStighi Data 2 martie 2017 19:59:20
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");

const int NMax = 100005;

int N,NS,Root;
int K[NMax],Sol[NMax],Stack[NMax];

vector <int> G[NMax];

void Read()
{
    fin>>N; Root = N * (N+1) / 2;

    for(int i = 1 ; i <= N ; ++i)
        fin>>K[i];

    for(int i = 1 ; i < N ; ++i)
    {
        int x,y;    fin>>x>>y;
        G[x].push_back(y);
        Root -= y;
    }
}

void DFS(int Nod)
{
    if(K[Nod])  Sol[Nod] = Sol[Stack[NS - K[Nod] + 1]] + 1;

    Stack[++NS] = Nod;

    for(int i = 0 ; i < (int) G[Nod].size() ; ++i)
    {
        int Vecin = G[Nod][i];
        DFS(Vecin);
    }

    NS--;
}

void Solve()
{
    DFS(Root);
}

void Print()
{
    for(int i = 1 ; i <= N ; ++i)
        fout<<Sol[i]<<" ";
}

int main()
{
    Read();
    Solve();
    Print();

    fin.close();
    fout.close();
    return 0;
}