Cod sursa(job #3281025)

Utilizator CimpoesuFabianCimpoesu Fabian George CimpoesuFabian Data 28 februarie 2025 08:46:03
Problema Asmax Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");

int n, v[16005], viz[16005], suma[16005];
vector <int> G[16005];

void DFS(int nod)
{
    viz[nod] = 1;
    for (auto next : G[nod])
        if (viz[next] == 0)
            DFS(next);
}

void DFShatz(int nod)
{
    viz[nod] = 1;
    suma[nod] = v[nod];
    for (auto next : G[nod])
        if (viz[next] == 0)
    {
        DFShatz(next);
        if (suma[next] > 0)
            suma[nod] += suma[next];
    }
}

int main()
{
    int i, j, x, y, cnt = 0, root;
    fin >> n;
    for (i = 1 ; i <= n ; i++)
        fin >> v[i];
    for (i = 1 ; i < n ; i++)
    {
        fin >> x >> y;
        G[x].push_back(y);
    }
    for (i = 1 ; i <= n ; i++)
    {
        cnt = 0;
        DFS(i);
        for (j = 1 ; j <= n ; j++)
            cnt += viz[j];
        if (cnt == n)
            root = i;
        for (j = 1 ; j <= n ; j++)
            viz[j] = 0;
    }
    DFShatz(root);
    fout << suma[root];
    return 0;
}