Cod sursa(job #553882)

Utilizator rares192Preda Rares Mihai rares192 Data 14 martie 2011 13:22:32
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;

#define NMAX 16005

vector<vector<int> > a;
int t[NMAX];
long long nod[NMAX];
int n, root;
bool s[NMAX];

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

int main()
{
	Read();
	Solve();
	Write();
	return 0;
}

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

void Solve()
{
	for(int i = 1; i <= n; ++i) if( t[i] == 0) root = i;
		
	DF(root);
}

void DF(int x)
{
	s[x] = 1;
	
	for(int i = 0; i < (int)a[x].size(); ++i)
	{
		int l = a[x][i];
		if( !s[ l ] )
		{
			s[ l ] = 1;
			DF( l );
			if( nod[x] < nod[l] + nod[x] )
				nod[x] = nod[l] + nod[x];
		}
	}
}


void Write()
{
	ofstream fout("asmax.out");
	
	const long long INF = 1000000000000000000LL;
	long long maxim = -INF;
	
	for(int i = 1; i <= n; ++i)
		if( nod[i] > maxim)
			maxim = nod[i];
	
	fout << maxim;
		
	fout.close();
}