Cod sursa(job #1001394)

Utilizator marePopescu Mihaela mare Data 24 septembrie 2013 21:45:07
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include<fstream>
#include<vector>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
bool viz[16003];
vector <int> a[16003];
int v[16003],x,y,sum[16003],max1,n;
void citire()
{
	f>>n;
	for(int i=1;i<=n;i++)
		f>>v[i];
	for(int i=0;i<n-1;i++)
		{

	f>>x>>y;
			a[x].push_back(y);
			a[y].push_back(x);
		}
}
void dfs(int x)
{
    viz[x]=1; sum[x]=v[x];

    for (int i=0; i<a[x].size(); i++)
        if (!viz[a[x][i]])
        {
            dfs(a[x][i]);
            if (sum[a[x][i]]>0)
                sum[x]=sum[x]+sum[a[x][i]];
        }
    if (sum[x]>max1) max1=sum[x];
}
int main()
{
	citire ();
	dfs(1);
	g<<max1;
	return 0;
}