Cod sursa(job #1105221)

Utilizator ion824Ion Ureche ion824 Data 11 februarie 2014 17:00:34
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 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;
      
     stack[++vf]=nod1;
      
     if(nr[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; 
}