Pagini recente » Cod sursa (job #141623) | Cod sursa (job #1375205) | Cod sursa (job #199272) | Cod sursa (job #119650) | Cod sursa (job #2669660)
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
using VI = vector<int>;
using VB = vector<bool>;
using VVI = vector<VI>;
VVI G;
VB s; // selecteaza nodurile vizitate
VI v; // valorile asociate nodurilor
VI smax; // smax[x] = suma maxima a unui subarbore cu radacina in nodul x
int Smax = -(16e6 + 1);
int n;
void CitesteArb();
void Df(int x);
int main()
{
CitesteArb();
Df(1);
for (int node = 1; node <= n; ++node)
if (smax[node] > Smax)
Smax = smax[node];
fout << Smax;
}
void Df(int x)
{
s[x] = true;
smax[x] = v[x];
for (const int& y : G[x])
{
if (s[y]) continue;
Df(y);
if (smax[y] > 0)
smax[x] += smax[y];
}
}
void CitesteArb()
{
fin >> n;
G = VVI(n + 1); // acum exista G[0], G[1], ..., G[n]
s = VB(n + 1);
smax = v = VI(n + 1);
for (int i = 1; i <= n; ++i)
fin >> v[i];
int x, y;
for (int i = 1; i < n; ++i)
{
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
}