Cod sursa(job #1060768)

Utilizator mvcl3Marian Iacob mvcl3 Data 18 decembrie 2013 17:55:50
Problema Cerere Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.99 kb
#include <fstream>
#include <stack>
#include <vector>

#define in "cerere.in"
#define out "cerere.out"
#define Max_Size 100009

std :: ifstream f(in);
std :: ofstream g(out);

int N;
int K[Max_Size];
int Sol[Max_Size], Tata[Max_Size], D[Max_Size];
bool Viz[Max_Size];

std :: vector < int > V[Max_Size];
std :: stack < std :: pair <int, int> > my_Stack;

inline void Read_Data()
{
    f >> N;
    for(int i = 1; i <= N; ++i) f >> K[i];

    for(int a, b, i = 1; i < N; ++i)
    {
        f >> a >> b;

        Tata[b] = a;
        V[a].push_back(b);
    }
}

void DFS(int node, int Nivel)
{
    Viz[node] = 1;
    D[Nivel] = node;

    if(K[node]) Sol[node] = Sol[ D[Nivel - K[node] ]] + 1;

    for(auto val : V[node])
        if(!Viz[val])
            DFS(val, Nivel + 1);
}

int main()
{
    Read_Data();

    for(int i = 1; i <= N; ++i)
        if(!Tata[i])
        {
            DFS(i, 1);
            break;
        }

    for(int i = 1; i <= N; ++i) g << Sol[i] << ' ';

    g.close();
    return 0;
}