Cod sursa(job #1579765)

Utilizator BrandonChris Luntraru Brandon Data 25 ianuarie 2016 08:22:41
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream cin ("asmax.in");
ofstream cout ("asmax.out");

vector <int> G[16005];
queue <int> q;

int cost[16005], n, maxx;
bool viz[16005], added;

void read()
{
    cin >> n;
    for(int i = 1; i <= n; ++i)
    {
        cin >> cost[i];
    }
    for(int i = 1; i <= n; ++i)
    {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
}

/*void bfs(int node)
{
    ///sum of whole tree, not just 1 branch
    viz[node] = true;
    q.push(node);
    while(q.empty() == false)
    {
        frn = q.front();
        if(added == true)
        {
            sum += cost[frn];
        }
        if(sum < 0)
        {
            sum = 0;
        }
        if(cost[frn] > 0)
        {
            added = true;
            sum += cost[frn];
        }
        q.pop();
        for(auto it: G[frn])
        {
            if(viz[it] == false and cost[it] > 0)
            {
                q.push(it);
            }
        }
    }
}*/

void dfs(int node)
{
    viz[node] = true;
    for(int i = 0; i < (int)G[node].size(); ++i)
    {
        int next_node = G[node][i];
        if(viz[next_node] == false)
        {
            dfs(next_node);
            if(cost[next_node] > 0)
            {
                cost[node] += cost[next_node];
            }
        }
    }
    if(cost[node] > maxx)
    {
        maxx = cost[node];
    }
}

void solve()
{
    dfs(1);
}

void print()
{
    cout << maxx << " ";
}

int main()
{
    read();
    solve();
    print();
    return 0;
}