Cod sursa(job #2012308)

Utilizator iuliagalataniulia galatan iuliagalatan Data 18 august 2017 14:44:20
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 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 = 0;
	for (const int& x : smax)
		res = max(res, x);
		
	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 = smax = VI(n + 1);
	G = vector<VI>(n + 1);
	
	for (int i = 1; i <= n; ++i)
		fin >> val[i];
	int x, y;
	while (fin >> x >> y)
	{
		G[x].push_back(y);
		G[y].push_back(x);
	}
}