Cod sursa(job #2779680)

Utilizator Irina.comanIrina Coman Irina.coman Data 4 octombrie 2021 18:49:57
Problema Asmax Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <vector>
using namespace std;
vector<int> graph[16005];
int vis[16005], v[1005], s[10005], maxx = -1000;

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

int main()
{
    ifstream fin("asmax.in");
    ofstream fout("asmax.out");
    int n, x, y;
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> v[i];
    for (int i = 1; i < n; i++)
    {
        fin >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    for (int i = 1; i <= n; i++)
    {
        dfs(i);
        for (int j = 1; j <= n; j++)
            vis[j] = false;
    }
    fout << maxx;
    return 0;
}