Cod sursa(job #2500140)

Utilizator Anakin1001George Giorgiu Gica Anakin1001 Data 27 noiembrie 2019 12:00:05
Problema Asmax Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream>
#include <vector>
#include <cstring>

#define N 16000 + 1

using namespace std;

ifstream f ( "asmax.in" );
ofstream g ( "asmax.out" );

vector < int > graph[N];
bool viz[N];
int cost[N], val[N], S;

void dfs ( int node ){
    viz[node] = 1;
    for ( int i = 0; i < graph[node].size(); i++ ){
        int new_node = graph[node][i];
        if ( viz[new_node] == 0 ){
            dfs ( new_node );
            cost[node] = max ( cost[node], val[node] + cost[new_node] );
        }
    }
}

void calc ( int node ){
    viz[node] = 1;
    S += val[node];
    for ( int i = 0; i < graph[node].size(); i++ ){
        int new_node = graph[node][i];
        if ( viz[new_node] == 0 && S + cost[new_node] > S )
            calc ( new_node );
    }
}

int main()
{   int n, i, rad, x, y;
    f >> n;
    for ( i = 1; i <= n; i++ ){
        f >> val[i];
        cost[i] = val[i];
    }
    for ( i = 1; i <= n - 1; i++ ){
        f >> x >> y;
        graph[x].push_back ( y );
        graph[y].push_back ( x );
    }
    dfs ( 1 );
    memset ( viz, 0, sizeof ( viz ) );
    calc ( 1 );
    g << S;
    return 0;
}