Cod sursa(job #1908288)

Utilizator vasilicamoldovanVasilica Moldovan vasilicamoldovan Data 7 martie 2017 00:16:10
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <vector>
using namespace std;

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

using VI = vector<int>;
using VB = vector<bool>;
using VVI = vector<VI>;

VVI G;
vector<bool> s;
VI smax;  // smax[x] = suma maxima a unui subarbore care contine si nodul x, continut in subarborele cu rad in x 
VI v;     // valorile asociate nodurilor
int n;

void ReadArb();
void Df(int x);

int main()
{
	ReadArb();
	Df(1);
	
	int Smax = 0;
	for (int i = 1; i <= n; ++i)
		Smax = max(Smax, smax[i]);
		
	fout << Smax;
	fin.close();
	fout.close();
}

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

void ReadArb()
{
	fin >> n;
	
	v = smax = VI(n + 1);
	G = VVI(n + 1);
	s = VB(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);
	}
	
}