Cod sursa(job #1597977)

Utilizator killlerr1Chilom Mircea killlerr1 Data 12 februarie 2016 15:27:01
Problema Asmax Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream is("asmax.in");
ofstream os("asmax.out");

int n, v[16001];
vector<vector<int>> G;
int smax[16001], sum = -999999;

void Read();

void Dfs(int nod);
bool vis[16001];

void Write();

int main()
{
    Read();
    /*for( int i = 1; i <= n; ++i )
        if( t[i] == 0 )
        {
            Dfs(i);
            break;
        }
        */
    Dfs(1);
    Write();

    return 0;
}

void Read()
{
    int x, y;
    is >> n;
    G.resize(n+1);
    for( int i = 1; i <= n; ++i )
    {
        is >> v[i];
        smax[i] = v[i];
    }
    while( is >> x >> y )
    {
        G[x].push_back(y);
        G[y].push_back(x);
    }
    is.close();
}

void Dfs(int nod)
{
    for( const auto& e : G[nod] )
        if( vis[e] == false && e)
        {
            vis[e] = true;
            Dfs(e);
            if( smax[e] > 0 )
            smax[nod] += smax[e];
        }

}

void Write()
{
    for( int i = 1; i <= n; ++i )
        if( smax[i] > sum )
            sum = smax[i];
    os << sum << '\n';
    os.close();
}