Cod sursa(job #2830417)

Utilizator TUdOr73Minciunescu Tudor TUdOr73 Data 9 ianuarie 2022 21:20:45
Problema Asmax Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

const int NMAX = 16003;
int costMax = -32000;
bool visited[NMAX];
int costs[NMAX];
int dfs(int node, vector<vector<int>> edges) {
    visited[node] = true;
    int cost = costs[node];

    for(auto neighbour: edges[node])
    {
        if(!visited[neighbour] ){
            cost = max(cost, cost + dfs(neighbour, edges));
        }
    }
    if(cost > costMax )
        costMax = cost;
    return cost;
}

int main()
{
    vector<vector<int>> edges;

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

    int n, x, y;
    f >> n;
    for(int i = 0; i< n; i++) {
        f >> x;
        costs[i+1] = x;
    }
    edges.resize(n+1);
    for(int i = 0; i< n-1; i++) {
        f >> x >> y;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }
    dfs(1, edges);
    g << costMax;
}