Cod sursa(job #892461)

Utilizator antonioteoZait Teodor Antonio antonioteo Data 26 februarie 2013 09:50:05
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <vector>
using namespace std;
const char iname[] = "asmax.in";
const char oname[] = "asmax.out";
ifstream fin(iname);
ofstream fout(oname);
int N, i, j, n, x, y, ANS;
int Vertex_cost[ 16004 ];
int dp[ 16004 ];
vector <int> Graph[ 16004 ];
bool viz[ 16004 ];
void DFS(int Tree_Root)
{
	dp[Tree_Root] = Vertex_cost[Tree_Root];
	vector <int> :: iterator Current_Leaf;
	viz[Tree_Root] = true;
	for (Current_Leaf = Graph[Tree_Root].begin(); Current_Leaf != Graph[Tree_Root].end(); ++Current_Leaf)
	{
		if (!viz[*Current_Leaf])
		{
			DFS(*Current_Leaf);
			if (dp[*Current_Leaf] > 0)
				dp[Tree_Root] += dp[*Current_Leaf];
		}
	}
	ANS = max(ANS, dp[Tree_Root]);
}
int main()
{
	ANS = (-1) * 0x3f3f3f3f;
	fin >> N; n = N - 1;
	for (i = 1; i <= N; ++i) fin >> Vertex_cost[i];
	while (n--)
	{
		fin >> x >> y;
		Graph[x].push_back(y), Graph[y].push_back(x);
	}
	DFS(1);
	fout << ANS << '\n';
	return 0;
}