Cod sursa(job #2806229)

Utilizator namesurname01Name Surname namesurname01 Data 22 noiembrie 2021 14:24:01
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <fstream>
#include <iostream>
#define N 16002
#include <vector>
#include <deque>
#define INF 2000000000000000

using namespace std;

int c[N];
vector <int> graph[N];
bool viz[N];
long long sum[N];
long long ma = -INF;
void low(int nod)
{
    int ve;
    sum[nod] = c[nod];
    viz[nod] = 1;
    for (int i = 0;i < graph[nod].size();++i)
    {
        ve = graph[nod][i];
        if (!viz[ve])
        {
            low(ve);
            viz[ve] = 1;
            sum[nod] = max(sum[nod], sum[nod] + sum[ve]);
        }
    }
}
int main()
{
    ifstream f("asmax.in");
    ofstream g("asmax.out");
    int n, x, y, ss = 0;
    f >> n;
    for (int i = 1;i <= n;++i) f >> c[i], ss += c[i];
    for (int i = 1;i <= n;++i)
    {
        f >> x >> y;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    low(1);
    for (int i = 1;i <= n;++i)
        ma = max(ma, sum[i]);
    g << ma;
    f.close();
    g.close();
    return 0;
}