Cod sursa(job #1257357)

Utilizator toniobFMI - Barbalau Antonio toniob Data 7 noiembrie 2014 17:43:09
Problema Cerere Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
# include <fstream>

using namespace std ;

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


int n , k [ 100002 ] , t [ 100002 ] ;

void read (   ) {
    in >> n ;

    for ( int i = 1 ; i <= n ; ++ i ) {
        in >> k [ i ] ;
    }

    for ( int i = 1 , a , b ; i <= n - 1 ; ++ i ) {
        in >> a >> b ;
        t [ b ] = a ;
    }
}

void writeanswer (  ) {
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( k [ i ] == 0 ) {
            out << "0 " ;

            continue ;
        }

        int ans = i , cnt = 0 ;

        while ( k [ ans ] != 0 ) {
            ++ cnt ;
            int y = ans ;
            for ( int h = 1 ; h <= k [ ans ] ; ++ h ) {
                y = t [ y ] ;
            }
            ans = y ;
        }

        out << cnt << " " ;
    }
}

int main (  ) {

    read (  ) ;

    writeanswer (  ) ;

}