Pagini recente » Cod sursa (job #168300) | Cod sursa (job #1352548) | Cod sursa (job #1633178) | Cod sursa (job #3290640) | Cod sursa (job #1016279)
#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;
if(b[n]) sol[n]=sol[stk[stk[0]-b[n]]]+1;
else sol[n]=0;
for(p=a[n];p;p=p->next)
{
dfs(p->n);
}
stk[0]--;
}
int main()
{
freopen("cerere.in", "r", stdin);
freopen("cerere.out", "w", stdout);
int n, i, s=0, x, y;
scanf("%d", &n);
for(i=1;i<=n;i++)
{
scanf("%d", &b[i]);
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]);
}
}