Cod sursa(job #603189)

Utilizator dacyanMujdar Dacian dacyan Data 14 iulie 2011 23:57:05
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <vector>
#define DIM 16100
#define INF 0x3f3f3f3f
using namespace std;

int n, a[DIM], smax(-INF);
vector<int> G[DIM];
bool s[DIM];
int d[DIM];

void Read();
void DF(int);
void Write();

int main()
{
	Read();
	DF(1);
	for (int i = 1; i <= n; ++i)
		if (d[i] > smax) smax = d[i];
	Write();
	return 0;
}

void Read()
{
	ifstream fin("asmax.in");
	fin >> n;
	for (int i = 1; i <= n; ++i)
		fin >> a[i];
	int x, y;
	for (int i = 1; i < n; ++i)
	{
		fin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	fin.close();
}

void DF(int nod)
{
	s[nod] = 1;
	d[nod] = a[nod];
	for (int i = 0; i < G[nod].size(); ++i)
	{
		int son = G[nod][i];
		if (s[son]) continue;
		DF(son);
		if (d[son] > 0)
			d[nod] += d[son];
	}
}

void Write()
{
	ofstream fout("asmax.out");
	fout << smax << '\n';
	fout.close();
}