Cod sursa(job #1365209)

Utilizator fetti_danutzdezactivat fetti_danutz Data 28 februarie 2015 10:29:38
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

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

const int MaxN = 16001, Inf = 0x3f3f3f3f;
vector<int> arb[MaxN];
vector<int> smax;  // smax[x] suam maxima a unui subarbore cu radacina in x
vector<int> v;     // valorile asociate nodurilor
vector<bool> s;
int n;

void Read();
void Df(int x);

int main()
{
	Read();
	Df(1);
	int vmax = -Inf;
	for (int i = 1; i <= n; ++i)
		vmax = max(vmax, smax[i]);
	
	fout << vmax << '\n';
	fin.close();
	fout.close();
}

void Df(int x)
{
	s[x] = true;
	smax[x] = v[x];
	for (const int& y : arb[x])
		if ( !s[y] )
		{
			Df(y);
			if ( smax[y] > 0 )
				smax[x] += smax[y];
		}
}

void Read()
{
	fin >> n;
	v.resize(n + 1);
	s.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;
		arb[x].push_back(y);
		arb[y].push_back(x);
	}
}