Cod sursa(job #3030639)

Utilizator adelahabanHaban Adela adelahaban Data 17 martie 2023 19:33:25
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <vector>
using namespace std;

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

using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;

int n;
int mx = -0x3f3f3f3f;
VVI a;
VI w;
VB v;
VI sz;

void CitesteDate();
void Dfs(int x);

int main()
{
    CitesteDate();
    Dfs(1);
    cout << mx;
}

void Dfs(int x)
{
    v[x] = true;
    sz[x] = w[x];

    for (int y : a[x])
    {
        if (v[y]) continue;

        Dfs(y);
        
        if (sz[y] > 0)
            sz[x] += sz[y];
    }

    mx = max(sz[x], mx);
}

void CitesteDate()
{
    cin >> n;
    w = sz = VI(n + 1);
    a = VVI(n + 1);
    v = VB(n + 1);

    for(int i = 1; i <= n; ++i)
        cin >> w[i];
    
    for (int x, y; cin >> x >> y; )
    {
        a[x].push_back(y);
        a[y].push_back(x);
    }   
}