Cod sursa(job #539192)

Utilizator brainwashed20Alexandru Gherghe brainwashed20 Data 22 februarie 2011 16:44:07
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include<cstdio>
#include<vector>

using namespace std;

#define Nmax 100001

vector<int> G[Nmax];
int T[Nmax], K[Nmax], D[Nmax], S[Nmax], N;
char viz[Nmax];

void DF(int nod, int nivel) {
	viz[nod]=1; S[nivel]=nod;
	if(!K[nod])
		D[nod]=0;
	else
		D[nod]=1+D[S[nivel-K[nod]]];
	for(vector<int>:: iterator it=G[nod].begin(), it2=G[nod].end(); it!=it2; ++it) 
		if(!viz[*it]) 
			DF(*it, nivel+1);
}
int main() {
	freopen("cerere.in","r",stdin);
	freopen("cerere.out","w",stdout);
	
	int i, x, y;
	
	scanf("%d",&N);
	for(i=1; i<=N; i++)
		scanf("%d",&K[i]);
	for(i=1; i<N; i++) {
		scanf("%d %d",&x,&y);
		G[x].push_back(y);
		T[y]=x;
	}
	for(i=1; i<=N; i++)
		if(!T[i])
			DF(i,0);
	
	for(i=1; i<=N; i++)
		printf("%d ",D[i]);
	printf("\n");
	
	return 0;
}