Pagini recente » Cod sursa (job #3127837) | Cod sursa (job #864259) | Cod sursa (job #575841) | Cod sursa (job #1916884) | Cod sursa (job #2817520)
#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;
}