Cod sursa(job #644013)

Utilizator thesilverhand13FII Florea Toma Eduard thesilverhand13 Data 5 decembrie 2011 02:08:27
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
 # include <fstream>
 # include <vector>

 # define dim 16005
 # define pb push_back
 
 using namespace std;

 int n, sol = -999999;
 int v[ dim ], viz[ dim ], cost[ dim ];
 vector <int> a[ dim ];
 
 ifstream f("asmax.in");
 ofstream g("asmax.out");

void dfs( int nod )
{
	
	int i;
    viz[ nod ] = 1;
    cost[ nod ] = v[ nod ];
	for ( i = 0 ; i < a[ nod ].size() ; i++ )
		if ( viz[ a[ nod ][ i ] ] == 0)
		{
           dfs( a[ nod ][ i ] );
            if ( cost[ a[ nod ][ i ] ] > 0 ) 
				cost[ nod ] += cost[ a[ nod ][ i ] ];
        }
}

void citire()
{
	int i, x, y;
	f >> n;
	for ( i = 1; i <= n; ++i) 
		f >> v[i];
    for ( i = 1; i < n; ++i)
    {
        f >> x >> y;
        a[ x ].pb(y);
        a[ y ].pb(x);
    }
}

void rezolva()
{
	int i;
	dfs(1);
	for ( i = 1; i <= n; ++i )
		sol = max(sol, cost[i]);

}

void afisare()
{
	g << sol << "\n";
}
int main()
{
	citire();
	rezolva();
	afisare();
    return 0;
}