Cod sursa(job #2012311)

Utilizator iuliagalataniulia galatan iuliagalatan Data 18 august 2017 14:52:53
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb

#include <fstream>
#include <vector>
using namespace std;

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

using VI = vector<int>;

vector<bool> v;
VI smax, val;
vector<VI> G;
int n;

void ReadGraph();
void Df(int x);

int main()
{
	ReadGraph();
	Df(1);
	
	int res = -1001;
	
	for (int i = 1; i <= n; ++i)
		res = max(res, smax[i]);

	fout << res;
	
	fin.close();
	fout.close();
}

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


void ReadGraph()
{
	fin >> n;
	v = vector<bool>(n + 1);
	val = VI(n + 1);
	smax = VI(n + 1);
	G = vector<VI>(n + 1);
	
	for (int i = 1; i <= n; ++i)
		fin >> val[i];
	int x, y;
	for (int i = 1; i < n; ++i)
	{
		fin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
}