Pagini recente » Cod sursa (job #402567) | Cod sursa (job #848655) | Cod sursa (job #971821) | Cod sursa (job #341866) | Cod sursa (job #1152993)
/*
Keep It Simple!
*/
#include<stdio.h>
#include<list>
#define MaxN 100001
using namespace std;
int N;
list<int> G[MaxN];
bool InLines[MaxN];
int root,k[MaxN],Dp[MaxN];
int Dads[MaxN];
void DFS(int node)
{
int cnt = k[node];
int aux = node;
while(cnt && aux != root)
{
aux = Dads[aux];
cnt--;
}
Dp[node] = Dp[aux]+1;
for(list<int>::iterator it = G[node].begin(); it!=G[node].end(); it++)
{
Dads[*it] = node;
DFS(*it);
}
}
int main()
{
freopen("cerere.in","r",stdin);
freopen("cerere.out","w",stdout);
scanf("%d",&N);
for(int i=1;i<=N;i++)
scanf("%d",&k[i]);
int x,y;
for(int i=2;i<=N;i++)
{
scanf("%d%d",&x,&y);
G[x].push_back(y);
InLines[y] = 1;
}
for(int i=1;i<=N;i++)
if(!InLines[i])
{
root = i;
break;
}
Dp[root] = k[root];
DFS(root);
for(int i=1;i<=N;i++)
printf("%d ",Dp[i]-1);
}