Cod sursa(job #1841154)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 5 ianuarie 2017 13:19:01
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <vector>
#define VAL 16005
#define INF 1000000000

using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");

int N, i, j;
int v[VAL], a, b;
int dp[VAL], best;
bool viz[VAL];
vector<int> G[VAL];

void dfs(int nod)
{
    vector<int> :: iterator it;
    viz[nod]=true;
    dp[nod]=v[nod];
    for (it=G[nod].begin(); it!=G[nod].end(); it++)
    {
        if (viz[*it]==false)
        {
            dfs(*it);
            dp[nod]=max(dp[nod], dp[nod]+dp[*it]);
        }
    }
    best=max(best, dp[nod]);
}

int main()
{
    fin >> N;
    for (i=1; i<=N; i++)
      fin >> v[i];
    for (i=1; i<N; i++)
    {
        fin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    best=-INF;
    dfs(1);
    fout << best << '\n';
    fin.close();
    fout.close();
    return 0;
}