Cod sursa(job #1892664)

Utilizator iuliastoianIulia Stoian iuliastoian Data 25 februarie 2017 10:49:53
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
#include <vector>
using namespace std;

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

using VI = vector<int>;
using VVI = vector<VI>;
int n;
VVI G;
VI smax;
VI v;
VI viz;
void Read();
void Df(int x);

int main()
{
	Read();
	Df(1);
	int Smax = 0;
	for (const int& val : smax)
		Smax = max(Smax, val);
	fout << Smax;

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

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

void Read()
{
	int x, y;
	fin >> n;
	G = VVI(n + 1);
	smax = v = viz = VI(n + 1);
	for (int i = 1; i <= n; ++i)
		fin >> v[i];

	while (fin >> x >> y)
		G[x].push_back(y),
		G[y].push_back(x);
}