Cod sursa(job #2817520)

Utilizator AlexandraBoghiuAlexandra Boghiu AlexandraBoghiu Data 13 decembrie 2021 19:30:37
Problema Asmax Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
vector<vector<int>> listOfNeighbours;
vector<int> visited, values, currentValue;
int maxValue = -100000;
void DFS(int node)
{
    visited[node] = 1;
    currentValue[node] = values[node];

    for (auto neighbour : listOfNeighbours[node])
        if (!visited[neighbour])
        {
            DFS(neighbour);
            if (currentValue[neighbour] > 0)
                currentValue[node] += currentValue[neighbour];
        }
    maxValue = max(maxValue, currentValue[node]);
}
int main()
{
    int number, x, firstNode, secondNode;
    in >> number;

    for (int i = 0; i < number; i++)
    {
        in >> x;
        values.push_back(x);
    }

    listOfNeighbours.resize(number + 1);
    visited.resize(number + 1);
    fill(visited.begin(), visited.end(), 0);
    currentValue.resize(number + 1);

    for (int i = 0; i < number; i++)
    {
        in >> firstNode >> secondNode;
        listOfNeighbours[firstNode].push_back(secondNode);
        listOfNeighbours[secondNode].push_back(firstNode);
    }

    DFS(1);
    out << maxValue;
    return 0;
}