Cod sursa(job #1875868)

Utilizator preda.andreiPreda Andrei preda.andrei Data 11 februarie 2017 18:18:33
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
#include <vector>

using namespace std;

struct Node
{
    int best_sum;
    bool visited = false;
    vector<int> edges;
};

typedef vector<Node> Tree;

void Dfs(Tree &t, int node)
{
    t[node].visited = true;
    for (int neighbour : t[node].edges) {
        if (!t[neighbour].visited) {
            Dfs(t, neighbour);
            t[node].best_sum += max(t[neighbour].best_sum, 0);
        }
    }
}

int FindRes(Tree &t, int root)
{
    Dfs(t, root);

    int res = -(1 << 30);
    for (const auto &node : t) {
        res = max(res, node.best_sum);
    }
    return res;
}

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

    int n;
    fin >> n;

    Tree tree(n);
    for (int i = 0; i < n; ++i) {
        fin >> tree[i].best_sum;
    }

    for (int i = 1; i < n; ++i) {
        int x, y;
        fin >> x >> y;
        tree[x - 1].edges.push_back(y - 1);
        tree[y - 1].edges.push_back(x - 1);
    }

    fout << FindRes(tree, 0) << "\n";
    return 0;
}