Cod sursa(job #2050028)

Utilizator OldpugAlex Ionescu Oldpug Data 27 octombrie 2017 21:51:04
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <bits/stdc++.h>

std::vector<int> graph[16005], dyn;
bool seen[16005];
int val[16005], max = -0x3f3f3f3f;

void dfs(int x)
{
  seen[x] = true;
  dyn[x] = val[x];

  for (auto y : graph[x])
    if (!seen[y])
    {
      dfs(y);
      dyn[x] = std::max(dyn[x], dyn[x] + dyn[y]);
    }

  max = std::max(max, dyn[x]);
}

int main()
{
  std::ifstream in("asmax.in");

  int n;
  in >> n;

  for (auto i = 1; i <= n; ++i)
    in >> val[i];

  for (auto i = 1; i <= n; ++i)
  {
    int x, y;
    in >> x >> y;

    graph[x].push_back(y);
    graph[y].push_back(x);
  }

  dyn = std::vector<int>(n + 1, -0x3f3f3f3f);
  dfs(1);

  std::ofstream("asmax.out") << max;
}