Cod sursa(job #2823530)

Utilizator LordNecrateBiowCuciureanu Dragos-Adrian LordNecrateBiow Data 28 decembrie 2021 19:10:42
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.15 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");


class Graph
{
private:
	bool _oriented;
	bool _weighted;

	int _nrNodes;
	int _nrEdges;

	vector < vector <int> > _neighborList;
	
	void read_asmax(vector<int>& values);
	void DFS_asmax(vector<bool>& visitedNodes, vector<int>& values, int currentNode);
	
public:
	Graph(int nrNodes = 0, int nrEdges = 0, bool oriented = false, bool weighted = false);
	~Graph() {}

	int maximumSubtreeCost();
};

Graph::Graph(int nrNodes, int nrEdges, bool oriented, bool weighted)
{
	_nrNodes = nrNodes;
	_nrEdges = nrEdges;

	_oriented = oriented;
	_weighted = weighted;

	vector <int> emptyVector;

	_neighborList.clear();

	for (int i = 0; i < nrNodes; i++)
	{
		_neighborList.push_back(emptyVector);
	}
}

void Graph::read_asmax(vector<int>& values)
{
	int node1, node2;

	for (int i = 0; i < _nrNodes; i++)
	{
		fin >> values[i];
	}

	for (int i = 0; i < _nrEdges; i++)
	{
		fin >> node1 >> node2;

		node1--;
		node2--;

		_neighborList[node1].push_back(node2);
		_neighborList[node2].push_back(node1);
	}
}

void Graph::DFS_asmax(vector<bool>& visitedNodes, vector<int>& values, int currentNode)
{
	visitedNodes[currentNode] = true;

	for (auto neighborNode : _neighborList[currentNode])
	{
		if (visitedNodes[neighborNode] == false)
		{
			DFS_asmax(visitedNodes, values, neighborNode);

			if (values[neighborNode] > 0)
			{
				values[currentNode] += values[neighborNode];
			}
		}
	}
}

int Graph::maximumSubtreeCost()
{
	vector<bool> visitedNodes(_nrNodes, false);
	vector<int> values(_nrNodes);
	int maxSum;

	read_asmax(values);

	DFS_asmax(visitedNodes, values, 0);

	maxSum = values[0];

	for (auto value : values)
	{
		maxSum = max(maxSum, value);
	}

	return maxSum;
}




int main()
{
	int nrNodes, maxSum;

	fin >> nrNodes;
	
	Graph graph(nrNodes, nrNodes - 1);
		
	maxSum = graph.maximumSubtreeCost();
	fout << maxSum;

	fin.close();
	fout.close();

	return 0;
}