Cod sursa(job #953189)

Utilizator GauGauGaurean Alexandru Florin GauGau Data 25 mai 2013 11:45:33
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");
typedef vector<int> VI;
typedef VI::iterator IT;

int n;
int maxx = -1001;
vector<int> G[16001];
bool s[16001];
int smax[16001];
void Read();
void Df(int x);

int main()
{
    Read();
    Df(1);
    for ( int i = 1; i <= n; ++i )
        if ( smax[i] > maxx )
            maxx = smax[i];
    fout << maxx;
    fin.close();
    fout.close();
    return 0;
}


void Read()
{
    fin >> n;
    int x, y;
    for (int i = 1; i <= n; ++i )
        fin >> smax[i];
    for ( int i = 1; i < n; ++i )
    {
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);

    }

}
void Df(int x)
{
    s[x] = true;
    for ( IT it = G[x].begin(); it != G[x].end(); ++it )
        if ( !s[*it] )
        {
            Df( *it );
            if ( smax[*it] > 0 )
                smax[x] += smax[*it];
    }
}