Cod sursa(job #182545)

Utilizator andrei_h5n1Haidau Andrei andrei_h5n1 Data 20 aprilie 2008 23:59:53
Problema Asmax Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <stdio.h>

using namespace std;

#define max(a,b) (a>b?a:b)
#define dim 16001

struct nod
{
	long inf;
	nod *next;
};
nod *l[dim];
long long v[dim], s[dim], n, m;

void add(int x, int y)
{
	nod *p=new nod;
	p->inf=x;
	p->next=l[y];
	l[y]=p;
}
void df(int);
int main()
{
	long i, x, y;

	freopen("asmax.in", "r", stdin);
	freopen("asmax.out", "w", stdout);

	scanf("%ld", &n);

	for(i=1; i<=n; i++)
		scanf("%ld", &v[i]);

	for(i=1; i<n; i++)
	{
		scanf("%ld%ld", &x, &y);
		add(x, y);
		add(y, x);
	}

   /*	for(i=1; i<=n; i++)
	{
		p=l[i];
		while(p)
		{
			printf("%ld ", p->inf);
			p=p->next;
		}
		printf("\n");

	}*/

	df(1);
	m=v[1];
	for(i=2; i<=n; i++)
		m=max(m, v[i]);

	printf("%ld", m);

	return 0;
}
void df(int i)
{
	nod *p=l[i];
	s[i]=1;
	while(p)
	{
		if(!s[p->inf])
		{
			df(p->inf);
			if(v[p->inf]>0)
				v[i]+=v[p->inf];
		}
		p=p->next;
	}
}