Cod sursa(job #643146)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 3 decembrie 2011 00:21:30
Problema Cerere Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <vector>

#define NMax 100005

using namespace std;

int N, R, S[NMax], K[NMax], F[NMax], Stack[NMax];
vector <int> G[NMax];

void Read ()
{
    ifstream fin ("cerere.in");
    fin >> N;
    for (int i=1; i<=N; ++i)
    {
        fin >> K[i];
    }
    R=N*(N+1)/2;
    for (int i=1; i<N; ++i)
    {
        int X, Y;
        fin >> X >> Y;
        G[X].push_back (Y);
        F[Y]=X;
        R-=Y;
    }
    fin.close ();
}

void DFS (int X, int L)
{
    if (K[X]==0)
    {
        S[X]=0;
    }
    else
    {
        S[X]=1+S[Stack[L-K[X]]];
    }
    Stack[L]=X;
    for (unsigned i=0; i<G[X].size (); ++i)
    {
        int V=G[X][i];
        DFS (V, L+1);
    }
}

void Print ()
{
    ofstream fout ("cerere.out");
    for (int i=1; i<=N; ++i)
    {
        fout << S[i] << " ";
    }
    fout << "\n";
    fout.close ();
}

int main()
{
    Read ();
    DFS (R, 1);
    Print ();
    return 0;
}