Cod sursa(job #134555)

Utilizator floringh06Florin Ghesu floringh06 Data 11 februarie 2008 21:07:43
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <cstdio>
#include <cstdlib>

using namespace std;

#define FIN "cerere.in"
#define FOUT "cerere.out"
#define MAX_N 100005

typedef struct nod
{
        int c;
        nod *nt;
};

nod *A[MAX_N];
nod *B[MAX_N];
int N, i;
int V[MAX_N];
int S[MAX_N];
int T[MAX_N];

int h[MAX_N];
nod *X;

    void DF (int a, int b, int c)
    {
         T[c] = a;
         if (!V[a]) S[a] = 0;
            else S[a] = S[T[c - V[a]]] + 1;
         
         nod *d;
         d = new (nod);
         d = A[a];
         while (d->nt!=NULL)
         {
               d = d->nt;
               DF (d->c, b + 1, c + 1);
         }
    }
 
    void solve ()
    {
         int i, x, y;
         
         for (i = 1; i <= N; ++i)
         {
             A[i] = new (nod); A[i]->nt = NULL;
             B[i] = new (nod); B[i]->nt = A[i];
         }     
             
         for (i = 1; i < N; ++i)
         {
             scanf ("%d %d", &x, &y);
             h[y] = 1;
             X = new (nod);
             
             X->c = y; X->nt = NULL;
             B[x]->nt->nt = B[x]->nt = X;
         }
         
         for (i = 1; i < N; ++i)
             if (h[i] != 1) y = i;
         V[y] = S[y] = 0;
         DF (y, 0, 1);
    }
         

    int main ()
    {
        freopen (FIN, "r", stdin);
        freopen (FOUT, "w", stdout);
        
        scanf ("%d", &N);
        for (i = 1; i <= N; ++i)
            scanf ("%d", V + i);

        solve ();
        
        for (i = 1; i < N; ++i)
            printf ("%d ", S[i]);
        printf ("%d\n", S[N]);
        
        return 0;
    }