Cod sursa(job #1455924)

Utilizator cristian.caldareaCaldarea Cristian Daniel cristian.caldarea Data 29 iunie 2015 14:13:05
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

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

const int Dim = 100002;

int n;
bool v[Dim], Rad[Dim];
int lvl[Dim], sol[Dim], k[Dim], s;
vector <int> G[Dim];

void Read();
void Dfs(int x,int nivel);

int main()
{
    Read();
    Dfs(s, 1);
    for(int i = 1; i <= n; i++)
        fout << sol[i] << " ";
    fin.close();
    fout.close();
    return 0;
}

void Read()
{
    fin >> n;
    for ( int i = 1; i <= n; i++)
        fin >> k[i];
    int x, y;
    for( int i = 1; i < n; i++)
    {
        fin >> x >> y;
        G[x].push_back(y);
        Rad[y] = 1;
    }
    for( int i = 1; i <= n; i++)
        if( Rad[i] == 0 )
        {
            s = i;
            break;
        }
}


void Dfs(int x,int nivel)
{
    v[x] = 1;
    if( k[x] == 0 )
        lvl[nivel]=0;
    else
        lvl[nivel] = lvl[nivel - k[x]] + 1;
    sol[x] = lvl[nivel];
    for( auto y : G[x] )
    {
        if( v[y] == 0 )
            Dfs(y, nivel + 1);
    }
}