Cod sursa(job #235950)

Utilizator ProtomanAndrei Purice Protoman Data 26 decembrie 2008 13:31:33
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <stdio.h>
#include <algorithm>
#define mx 1000010

using namespace std;

struct nod
{
	int nr;
	nod *ua;
} *l[mx];
bool t[mx];
int alks[mx], ad[mx], stv[mx];
int n, x, y, rad_arb;

void clad(int t, int f)
{
	nod *p = new nod;
	p->nr = f;
	p->ua = l[t];
	l[t] = p;
}

void calc(int x, int lvl)
{
	stv[lvl] = x;
	ad[x] = ad[stv[lvl - alks[x]]] + 1;
	for (nod *p = l[x]; p; p = p->ua)
		calc(p->nr, lvl + 1);
}

int main()
{
	freopen("cerere.in","r",stdin);
	freopen("cerere.out","w",stdout);
	scanf("%ld", &n);
	for (int i = 1; i <= n; i++)
		scanf("%ld", &alks[i]);
	for (int i = 1; i < n; i++)
	{
		scanf("%ld %ld", &x, &y);
		t[y] = 1;
		clad(x, y);
	}
	for (int i = 1; i <= n; i++)
		if (!t[i])
			rad_arb = i;
	calc(rad_arb, 1);
	for (int i = 1; i <= n; i++)
		printf("%ld ", ad[i] - 1);
	fclose(stdin);
	fclose(stdout);
	return 0;
}