Cod sursa(job #2203004)

Utilizator vladsftVlad Safta vladsft Data 10 mai 2018 17:58:51
Problema Asmax Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <vector>

using namespace std;
const int N = 16001;
vector <int> a[N];
int pct[N], s[N], mx = -10000000, n;
bool viz[N];
ifstream f("asmax.in");
ofstream g("asmax.out");
void citire()
{
    int x, y;
    f >> n;
    for (int i = 1; i <= n; i++)
        f >> pct[i];
    for (int i = 1; i < n; i++)
    {
        f >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
    f.close();
}
void dfs(int x)
{
    viz[x] = true;
    for (int i = 1; i <= a[x].size(); i++)
    {
        int y = a[x][i];
        if (viz[y] == false)
        {
            dfs(y);
            if (pct[y] > 0)
                pct[x] += pct[y];
        }
    }
    if (mx < pct[x])
        mx = pct[x];
}
int main()
{
    citire();
    for (int j = 1; j <= n; j++)
    {
        if (viz[j] == false)
            dfs(j);
    }
    g << ++mx;
    g.close();
    return 0;
}