Cod sursa(job #3246034)

Utilizator razvanmrt_06Mariuta Razvan razvanmrt_06 Data 1 octombrie 2024 16:15:53
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <vector>

using namespace std;

vector<vector<int>> graph;
vector<int> asmax, v;

void dfs(int node, int father){
    asmax[node] = v[node];
    for(int son : graph[node]){
        if(son != father){
            dfs(son, node);
            if(asmax[son] > 0){
                asmax[node] += asmax[son];
            }
        }
    }
}

int main(){
    int n;
    ifstream fin("asmax.in");
    ofstream fout("asmax.out");
    fin >> n;
    graph = vector<vector<int>>(n+1, vector<int>());
    asmax = vector<int>(n+1);
    v.push_back(0);
    for(int i = 1; i <= n; i++){
        int x;
        fin >> x;
        v.push_back(x);
    }
    for(int i = 1; i <= n-1; i++){
        int x, y;
        fin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    dfs(1, 0);
    int sol = asmax[1];
    for(int i = 2; i <= n; i ++){
        sol = max(sol, asmax[i]);
    }
    fout << sol;
    return 0;
}