Cod sursa(job #789208)

Utilizator Coman95coman cosmin Coman95 Data 17 septembrie 2012 16:20:36
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

typedef vector<int> VI;

ifstream fin("asmax.in");
ofstream fout("asmax.out");

vector<VI> G;
vector<bool> s;
int n;
VI v;
VI smax;

void Read();
void Write();
void DF(int x);

int main()
{
	Read();
	DF(1);
	Write();
	fin.close();
	fout.close();
	return 0;
}
void DF( int x )
{
	s[x] = true;
	smax[x] = v[x];
	for( size_t i = 0; i < G[x].size(); ++i )
		if( !s[G[x][i]] )
		{
			DF( G[x][i] );
			if ( smax[G[x][i]] > 0 )
				smax[x] += smax[G[x][i]];
		}
			
}

void Read()
{
	fin >> n;
	G.resize( n + 1 );
	s.resize( n + 1 );
	v.resize( n + 1 );
	smax.resize( n + 1 );
	for( int i = 1; i <= n; ++i )
		fin >> v[i];
		
	int x, y;	
	for( int i = 1; i < n; ++i )
	{
			fin >> x >> y;
			G[x].push_back( y );
			G[y].push_back( x );
	}	
	
}
void Write()
{
	int maxx = 0;
	for( int i = 1; i <= n; ++i )
		maxx = max( maxx, smax[i] );
	fout << maxx << '\n';
}