Cod sursa(job #1059238)

Utilizator vladstoickvladstoick vladstoick Data 16 decembrie 2013 14:46:41
Problema Cerere Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <queue>
using namespace std;

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

const int N=100001;

vector<int> muchii[N];
int par[N],k[N],dist[N];
int n;

int gasesteVarf(){
    for(int i=1;i<=n;i++){
        if(k[i]==0){
            return i;
        }
    }
    return -1;
}

void bfs(int x,int nivel){
    par[nivel]=x;
    if(k[x]!=0){
        dist[x] = 1+ dist[par[nivel-k[x]]];
    }
    for(int i=0;i<muchii[x].size();i++){
        bfs(muchii[x][i],nivel+1);
    }
}

int main()
{
    in>>n;
    for(int i=1;i<=n;i++){
        in>>k[i];
    }
    for(int i=1;i<n;i++){
        int x,y;
        in>>x>>y;
        muchii[x].push_back(y);
    }
    bfs(gasesteVarf(),1);
    for(int i=1;i<=n;i++){
        out<<dist[i]<<" ";
    }
    return 0;
}