Cod sursa(job #714715)

Utilizator vendettaSalajan Razvan vendetta Data 15 martie 2012 23:28:36
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <vector>
#define nmax 100005

using namespace std;

int n, Ord[nmax], Stramos[nmax], St[nmax];
vector<int> gf[nmax];

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

void dfs(int nod, int niv){

    St[niv] = nod;
    if (Ord[nod] == 0) Stramos[nod] = 0;
        else Stramos[nod] = St[niv-Ord[nod]];

    for(int i=0; i<gf[nod].size(); i++){
        int vc = gf[nod][i];
        //St[niv] = vc;
        dfs(vc, niv+1);
    }

}

int main(){

    f >> n;
    for(int i=1; i<=n; i++) f>>Ord[i];

    for(int i=1; i<n; i++){
        int x,y;
        f >> x >> y;
        gf[x].push_back(y);
    }

    dfs(1, 1);

    for(int i=1; i<=n; i++){
        int rez = 0;
        for(int j=Stramos[i]; j; j=Stramos[j],++rez);
        g << rez << " ";
    }
    g << "\n";

}