Cod sursa(job #966560)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 26 iunie 2013 11:30:13
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

#define Nmax 16005

vector <int> cost(Nmax);
vector <int> arb[Nmax];
vector <int> sum(Nmax);
vector <bool> viz(Nmax);


int N, MAX = -(1<<20);

void read()
{
    ifstream f("asmax.in");

    f >> N;

    for ( int i = 1; i <= N; i++ )
            f >> cost[i];

    for ( int i = 1, a, b; i < N; ++i )
    {
            f >> a >> b;

            arb[a].push_back( b );
            arb[b].push_back( a );
    }

    f.close();
}

void DFS( int node )
{
    viz[node] = true;
    sum[node] = cost[node];

    for ( unsigned int i = 0; i < arb[node].size(); ++i )
    {
        if ( !viz[ arb[node][i] ] )
        {
            DFS( arb[node][i] );

            if ( sum[ arb[node][i] ] > 0 )
                sum[node] += sum[ arb[node][i] ];
        }
    }

    MAX = max( MAX, sum[node] );
}

void print()
{
    ofstream g("asmax.out");

    g << MAX << "\n";

    g.close();
}

int main()
{
    read();
    DFS(1);
    print();

    return 0;
}