Cod sursa(job #2044135)

Utilizator costi2Radu Canu costi2 Data 20 octombrie 2017 22:21:37
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <string.h>
#include <climits>

#define Nmax 16001
using namespace std;
vector <pair<int,int> > vec[Nmax];
int vazut[Nmax];
int v[Nmax];
ifstream in("asmax.in");
ofstream out("asmax.out");
int s = 0, ma = INT_MIN;
int dfs(int node)
{
        vazut[node] = 1;
        int valoare = v[node];
        int rez;
        for(auto e : vec[node])
        {
                if(vazut[e.first] == 0)
                {
                        rez = dfs(e.first);
                        if(rez > 0)
                                valoare += rez;
                }
        }
        if(valoare > ma)
                ma = valoare;
        return valoare;
}
int main()
{
        int N,x,y;
        in >> N;
        memset(vazut,0,sizeof(vazut));
        for(int i = 1; i  <= N; i++)
        {
                in >> x;
                v[i] = x;
        }
        for(int i = 0; i < N-1; i++)
        {
                in >> x >> y;
                vec[y].push_back(make_pair(x,v[x]));
                vec[x].push_back(make_pair(y,v[y]));
        }
        dfs(1);
        out << ma <<'\n';
        return 0;
}