Cod sursa(job #528306)

Utilizator dornescuvladVlad Eugen Dornescu dornescuvlad Data 2 februarie 2011 15:59:24
Problema Asmax Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

const char iname[] = "asmax.in";
const char oname[] = "asmax.out";
const int  nmax    = 16384;

ifstream fin(iname);
ofstream fout(oname);

long long dp[nmax];
vector<long long> Gr[nmax];
long long i, j, k, n, m, x, y, rad, maxk;
long long Cost[nmax], incoming[nmax], outcoming[nmax], tata[nmax], viz[nmax];

void DF(int nod)
{	
	maxk = -21891222;
	viz[nod] = 1;
	if(outcoming[nod] == 0)
		dp[nod] = Cost[nod];
	for(vector<long long>::iterator it = Gr[nod].begin(); it != Gr[nod].end(); ++it)
	{
		if(viz[*it] == 0)
		{	
			viz[*it] = 1;
			DF(*it);
			if(Cost[nod] < Cost[nod] + Cost[*it])
				Cost[nod] += Cost[*it];
		}
	}
	if(Cost[nod] > maxk)
		maxk = Cost[nod];
}
	


int main()
{
	fin >> n;
	for(i = 1; i <= n; i ++)
	{
		fin >> Cost[i];
		dp[i] = Cost[i];
	}
	for(i = 1; i <= n - 1; i ++)
	{
		fin >> x >> y;
		Gr[x].push_back(y);
		incoming[y]++;
		outcoming[x]++;
		tata[y] = x;
	}
	
	for(i = 1; i <= n; i ++)
		if(incoming[i] == 0)
		{
			rad = i;
			break;
		}
		
	DF(rad);	
	fout << maxk << "\n";
	return 0;
}