Pagini recente » Cod sursa (job #2298997) | Cod sursa (job #3230156) | Cod sursa (job #1755013) | Cod sursa (job #1582761) | Cod sursa (job #587814)
Cod sursa(job #587814)
#include <cstdio>
#include <string>
#define MAXN 100000
typedef struct Node {
int d;
Node *n;
} Node;
int main(){
freopen("cerere.in", "r", stdin);
freopen("cerere.out", "w", stdout);
int N, G[MAXN+10], i, a, b, qp, R[MAXN+10], Q[MAXN+10];
Node *T[MAXN+10], *aux;
memset(T, NULL, sizeof(T));
memset(R, -1, sizeof(R));
scanf("%d", &N);
for(i=1; i<=N; i++)
scanf("%d", G+i);
for(i=1; i<N; i++){
scanf("%d%d", &a, &b);
aux=new Node;
aux->d=b;
aux->n=T[a];
T[a]=aux;
}
qp=1; Q[1]=1; R[1]=0;
while(qp){
while(T[Q[qp]] != NULL){
Q[qp+1]=T[Q[qp]]->d;
R[Q[qp+1]]=R[Q[qp+1-G[Q[qp+1]]]]+1;
qp++;
}
qp--;
if(qp>0)
T[Q[qp]]=T[Q[qp]]->n;
}
for(i=1; i<=N; i++)
printf("%d ", R[i]);
printf("\n");
return 0;
}