Cod sursa(job #1491873)

Utilizator EpictetStamatin Cristian Epictet Data 26 septembrie 2015 12:16:13
Problema Asmax Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin ("asmax.in");
ofstream fout ("asmax.out");

int n, S[16010];
vector < int > V[16010];
bool F[16010];


void BFS(int nod)
{
    F[nod] = true;
    for (vector < int > :: iterator it = V[nod].begin(); it != V[nod].end(); it++)
    {
        if (!F[*it])
        {
            BFS(*it);
            if (S[*it] > 0)
            {
                S[nod] += S[*it];
            }
        }
    }
}

int main()
{
    fin >> n;
    for (int i = 1; i <= n; i++) fin >> S[i];
    for (int i = 1, x, y; i < n; i++)
    {
        fin >> x >> y;
        V[x].push_back(y);
        V[y].push_back(x);
    }

    for (int i = 1; i <= n; i++)
    {
        if (V[i].size() == 1)
        {
            BFS(i);
            fout << S[i] << '\n';
            break;
        }
    }

    fin.close();
    fout.close();
    return 0;
}