Cod sursa(job #2835542)

Utilizator TudosieRazvanTudosie Marius-Razvan TudosieRazvan Data 18 ianuarie 2022 20:44:42
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <unordered_map>
#include <cstring>
#include <climits>

#define NMAX 16001
using namespace std;

int n, m,maxim=INT_MIN;
int cost[NMAX];
bool viz[NMAX];
vector<int>graf[NMAX];

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



void dfs(int nod)
{
	viz[nod] = true;
	for (int i = 0; i < graf[nod].size(); i++)
	{
		int nd = graf[nod][i];
		if (!viz[nd])
		{
			dfs(nd);
			if (cost[nod] < cost[nod] + cost[nd])
			{
				cost[nod] = cost[nod] + cost[nd];
			}
		}
	}
	maxim = max(maxim, cost[nod]);
}

int main() {

	fin >> n;
	for (int i = 1; i <= n; i++)
	{
		fin >> cost[i];
		viz[i] = false;
	}
	for (int i = 1; i <= n; i++)
	{
		int x, y;
		fin >> x >> y;
		graf[y].push_back(x);
		graf[x].push_back(y);
	}
	dfs(1);
	fout << maxim;
	return 0;
}