Cod sursa(job #1016244)

Utilizator nicuvladNicu Vlad nicuvlad Data 25 octombrie 2013 22:31:40
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <cstdio>
#define N 100002
using namespace std;

struct graf
{
    int n;
    graf *next;
};

graf *a[N];
int b[N], sol[N], stk[N];

void gadd(int x, int y)
{
    graf *p=new graf;
    p->n=y;
    p->next=a[x];
    a[x]=p;
}

void dfs(int n)
{
    stk[++stk[0]]=n;
    graf *p;
    sol[n]=sol[stk[stk[0]-b[n]]];
    for(p=a[n];p;p=p->next)
    {
        dfs(p->n);
    }
    stk[0]--;
}

int main()
{
    freopen("cereri.in", "r", stdin);
    freopen("cereri.out", "w", stdout);
    int n, i, s=0, x, y;
    scanf("%d", &n);
    for(i=1;i<=n;i++)
    {
        scanf("%d", &n);
        s+=i;
    }
    for(i=1;i<n;i++)
    {
        scanf("%d%d", &x, &y);
        s-=y;
        gadd(x, y);
    }
    dfs(s);
    for(i=1;i<=n;i++)
    {
        printf("%d ", sol[i]);
    }
}