Cod sursa(job #2194492)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 13 aprilie 2018 16:40:20
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");

const int NMAX = 16e3 + 5;
int sol = -99999999;
vector <int> v[NMAX];
int viz[NMAX], cost[NMAX];

int get_max(int a, int b)
{
	if(a > b)
		return a;
	return b;
}

void DFS(int nod)
{
	viz[nod] = 1;
	for(int i = 0; i < v[nod].size(); i++)
	{
		int fiu = v[nod][i];
		if(viz[fiu] == 0)
		{
			DFS(fiu);
			if(cost[nod] < cost[nod] + cost[fiu])
				cost[nod] += cost[fiu];
			sol = get_max(sol, cost[nod]);
		}
	}
}

int main()
{
	int n;
	f >> n;
	for(int i = 1; i <= n; i++)
		f >> cost[i];
	for(int i = 1; i < n; i++)
	{
		int x, y;
		f >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	DFS(1);
	g << sol;
}