Cod sursa(job #712854)

Utilizator ion824Ion Ureche ion824 Data 13 martie 2012 21:01:35
Problema Cerere Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include<cstdio>
#define nmax 100005

typedef struct Lnod{
               int v;
               struct Lnod *next;
               }*nod;
nod a[nmax];  
int nr[nmax],rs[nmax],stack[nmax],n,k,i,vf;  
bool enter[nmax];        

void readdata(){
     freopen("cerere.in","r",stdin);
     int x,y; nod p;
     scanf("%d\n",&n);
     for(int i=1;i<=n;++i)scanf("%d ",&nr[i]);
     while(!feof(stdin)){
                    scanf("%d %d\n",&x,&y);
                    p = new Lnod;
                    p->v=y;
                    p->next=a[x];
                    a[x]=p;
                    enter[y]=1;
                    }
     fclose(stdin);               
     }

void scrie(){
    freopen("cerere.out","w",stdout);
    for(int i=1;i<=n;++i)printf("%d ",rs[i]);
    fclose(stdout);     
     }

void bfs(int nod1){
     nod p;
     
     if(nr[nod1]){
            stack[++vf]=nod1; 
            rs[nod1]=rs[stack[vf-nr[nod1]]]+1;    
                 }
     
      for(p=a[nod1]; p; p=p->next){ bfs(p->v); --vf; }               
     }

int main(void){
    readdata();
    i=1; while(enter[i] && i<=n)++i; 
    bfs(i);                   
    scrie();
   return 0; 
}