Cod sursa(job #2669650)

Utilizator BGondaGonda George Luis Bogdan BGonda Data 7 noiembrie 2020 15:08:48
Problema Asmax Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <algorithm>
#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;
VB s;      // selecteaza nodurile vizitate
VI v;      // valorile asociate nodurilor  
VI smax;   // smax[x] = suma maxima a unui subarbore cu radacina in nodul x
int Smax;
int n;

void CitesteArb();
void Df(int x);

int main()
{
	CitesteArb();
	Df(1);
	
	for (int node = 1; node <= n; ++node)
		if (smax[node] > Smax)
			Smax = smax[node];
	
	fout << Smax;
	 
}


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 CitesteArb()
{
	fin >> n;
	G = VVI(n + 1); // acum exista G[0], G[1], ..., G[n]
	s = VB(n + 1);
	smax = v = VI(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);
	}
}