Cod sursa(job #1892684)

Utilizator dumbraveanbDumbravean Bogdan dumbraveanb Data 25 februarie 2017 10:58:25
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

const int Inf = 0x3f3f3f3f;
int n, vmax;
vector<int> A, D;
vector<bool> P;
vector<vector<int>> G;

void Dfs(int x) {
    P[x] = true;
    D[x] = A[x];
    for(auto y : G[x])
        if(!P[y]) {
            Dfs(y);
            D[x] = max(D[x], D[y] + D[x]);
        }
    vmax = max(vmax, D[x]);
}

int main()
{
    fin >> n;
    A = vector<int> (n + 1);
    D = vector<int> (n + 1, -Inf);
    P = vector<bool> (n + 1);
    G = vector<vector<int>> (n + 1);
    for(int i = 1; i <= n; ++i)
        fin >> A[i];
    int x, y;
    for(int i = 1; i < n; ++i) {
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    Dfs(1);
    fout << vmax;
    return 0;
}