Cod sursa(job #2830730)

Utilizator HatersMcCristian Ioan HatersMc Data 10 ianuarie 2022 10:47:31
Problema Asmax Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
vector<vector<int>> a;
int n,x,y,maxim;
vector<int> c;
vector<bool> viz;
int dfs(int n){
    viz[n]= true;
    int suma=c[n];
    for(int x : a[n]){
        if(!viz[x]){
            suma=max(suma,suma+dfs(x));
        }
    }
    if(suma>maxim)
        maxim=suma;
    return suma;
}
int main() {
    f>>n;
    a.resize(n+1);
    c.resize(n+1);
    viz.resize(n+1,false);
    for(int i = 1 ; i <=n ;++i){
        f>>c[i];
    }
    for(int i=0 ; i<n-1;++i){
        f>>x>>y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfs(1);
        g<<maxim;
    return 0;
}