Cod sursa(job #2094953)

Utilizator PaulTPaul Tirlisan PaulT Data 26 decembrie 2017 19:15:11
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;

const int Inf = 0x3f3f3f3f;
VVI G;
VI v, s;
int n, root = 1, sol = -Inf;

void Read();
void Df(int x);
void Write();

int main()
{
    Read();
    Write();
}

void Df(int x)
{
    s[x] = v[x];
    for (const int& y : G[x])
        if (s[y] == -Inf)
        {
            Df(y);
            if (s[y] > 0)
                s[x] += s[y];
        }
}

void Write()
{
    ofstream fout("asmax.out");
    Df(root);
    for (int i = 1; i <= n; i++)
        sol = max(sol, s[i]);
    fout << sol;
    fout.close();
}

void Read()
{
    ifstream fin("asmax.in");
    fin >> n;
    G = VVI(n + 1);
    v = s = VI(n + 1, -Inf);
    for (int i = 1; i <= n; i++)
        fin >> v[i];
    int x, y;
    for (int i = 1; i < n; i++)
    {
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    fin.close();
}