Cod sursa(job #343906)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 27 august 2009 18:16:31
Problema Asmax Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include<cstdio>
#define N 16001
short int *a[N],n,x[N],y[N],d[N],v[N];
int s,max=-N;
void citire()
{
	freopen("asmax.in","r",stdin);
	freopen("asmax.out","w",stdout);
	scanf("%hd",&n);
	for (int i=1; i<=n; ++i)
	{
		scanf("%hd",&v[i]);
		/*if (max<v[i])
			max=v[i];
		s+=v[i];*/
	}
	/*if (max<s)
		max=s;*/
	s=0;
	for (int i=1; i<n; ++i)
	{
		scanf("%hd%hd",&x[i],&y[i]);
		++d[x[i]];
		++d[y[i]];
	}
	for (int i=1; i<=n; ++i)
	{
		a[i]=new short int [1+d[i]];
		a[i][0]=0;
	}
	for (int i=1; i<n; ++i)
	{
		a[x[i]][++a[x[i]][0]]=y[i];
		a[y[i]][++a[y[i]][0]]=x[i];
	}
}
short int coada[N];
bool viz[N];
void bfs(int x1)
{
	int u=0,p=0;
	short int x,y;
	coada[u++]=x1;
	viz[x1]=true;
	s=v[x1];
	if (s>max)
		max=s;
	while (u!=p)
	{
		x=coada[p++];
		for (int i=1; i<=a[x][0]; ++i)
		{
			y=a[x][i];
			if (!viz[y])
			{
				s+=v[y];
				if (s>max)
					max=s;
				viz[y]=true;
				coada[u++]=y;
			}
		}
	}
}
void refresh()
{
	for (int i=1; i<=n; ++i)
		viz[i]=false;
}
void graf()
{
	for (int i=1; i<=n; ++i)
	{
		bfs(i);
		refresh();
	}
	printf("%d",max);
}
int main()
{
	citire();
	graf();
	return 0;
}