Cod sursa(job #2474549)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 15 octombrie 2019 15:22:27
Problema Cerere Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#include <vector>
#define VAL 110000
using namespace std;
ifstream f ("cerere.in");
ofstream g ("cerere.out");
vector <int> L[VAL];
int i, st, n, x, y;
int v[VAL], t[VAL], D[VAL], K[VAL], nr[VAL], fr[VAL];

void dfs ( int nod, int niv ){
    v[nod] = 1;
    D[niv] = nod;
    nr[nod] = nr[D[niv-K[nod]]]+1;
    for ( int i = 0; i < L[nod].size(); i++ ){
        int vecin = L[nod][i];
        if ( !v[vecin] )
            dfs(vecin, niv+1);
    }
}


int main()
{
    f>>n;
    for ( i=1; i <= n; i++ ) f>>K[i];

    while ( f>>x>>y ){
        fr[y]++;
        L[x].push_back(y);
        L[y].push_back(x);
    }
    for ( i=1; i <= n; i++ )
        if ( !fr[i] )
            st = i;
    dfs ( st, 1 );
    for ( i=1; i <= n; i++ )
        g<<nr[i]-1<<" ";
    return 0;
}