Cod sursa(job #2781552)

Utilizator Irina.comanIrina Coman Irina.coman Data 9 octombrie 2021 19:53:42
Problema Asmax Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <iostream>
#include <vector>
using namespace std;
vector<int> graph[105];
int vis[105], v[1005], s[105];

void dfs(int node)
{
    vis[node] = true;
    s[node] += v[node];
    for (int i = 0; i < graph[node].size(); i++)
    {
        int next = graph[node][i];
        if (!vis[next])
        {
            dfs(next);
            if (v[next] > 0)
                s[node] += v[next];
        }
    }
}

int main()
{
    int n, x, y, rad, maxx = -1005;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i];
    }
    for (int i = 1; i < n; i++)
    {
        cin >> x >> y;
        graph[y].push_back(x);
        graph[x].push_back(y);
    }
    for (int i = 1; i <= n; i++)
    {
        dfs(i);
        maxx = max(maxx, s[i]);
        for (int j = 1; j <= n; j++)
        {
            vis[j] = false;
            s[i] = 0;
        }
    }
    cout << maxx;
    return 0;
}