Cod sursa(job #2781848)

Utilizator MindralexMindrila Alex Mindralex Data 10 octombrie 2021 17:03:08
Problema Aprindere Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <iostream>
#include <fstream>
 
using namespace std;
 
ifstream fin ("asmax.in");
ofstream fout ("asmax.out");
 
const int maxVal = 16001;
 
int tata[maxVal], s[maxVal], v[maxVal];
 
struct nod
{
  int info;
  nod * urm;
}* lista[maxVal];
 
void adaugare (int a, int b)
{
  nod * t = new nod;
  t -> info = b;
  t -> urm = lista[a];
  lista[a] = t;
}
 
void prelucrare (int x)
{
  s[x] = v[x];
  nod * p = lista[x];
  if (p)
  {
    while (p)
    {
      if (p -> info != tata[x])
      {
        prelucrare (p -> info);
        if (s[x] < s[x] + s[p -> info])
          s[x] = s[x] + s[p -> info];
      }
      p = p -> urm;
    }
  }
  
 
}
 
int main()
{
  int n,  i;
  fin >> n;
  for (i = 1; i <= n; i++)
    fin >> v[i];
  int a, b;
  while (fin >> a >> b)
  {
    adaugare(a, b);
    adaugare(b, a);
  }
  for (i = 1; i <= n; i++)
  {
    nod * p =  lista[i];
    while (p)
    {
      if (p -> info != tata[i])
        tata[p -> info] = i;
      p = p -> urm;
    }
  }
  prelucrare(1);
  int maxim = -16000001;
  for (int i = 1; i <= n; i++)
    if (s[i] > maxim)
      maxim = s[i];
  fout << maxim;
  return 0;
}