Cod sursa(job #1658770)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 21 martie 2016 19:42:05
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <iomanip>
#include <cstring>

#define NMax 16005
#define INF 0x3f3f3f3f
using namespace std;

int viz[NMax],sum[NMax],a[NMax];
vector<int> G[NMax];
queue<int> q;
int n,x,y;

void dfs(int nod){
    viz[nod] = 1;
    sum[nod] = a[nod];
    for(int i = 0; i < G[nod].size(); ++i){
        if(!viz[G[nod][i]]){
            dfs(G[nod][i]);
            if(sum[G[nod][i]] > 0)
                sum[nod] += sum[G[nod][i]];
        }
    }
}
int main()
{
    ifstream f("asmax.in");
    ofstream g("asmax.out");
    f >> n;
    for(int i = 1; i <= n; ++i)
        f >> a[i];
    for(int i = 1; i < n; ++i){
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    dfs(1);
    int mx = -INF;
    for(int i = 1; i <= n; ++i){
        if(sum[i] > mx)
            mx = sum[i];
    }
    g << mx;
    return 0;
}