Cod sursa(job #587814)

Utilizator Smaug-Andrei C. Smaug- Data 5 mai 2011 22:42:35
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#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;
  
}