Cod sursa(job #2952571)

Utilizator NiffSniffCojocaru Calin Marcu NiffSniff Data 9 decembrie 2022 15:52:44
Problema Asmax Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
const int N = 1000000;
string file = "asmax";
ifstream cin(file +".in");
ofstream cout(file +".out");
vector <int> a[N + 1],v;
bitset <N + 1> viz;
int n, cnt, smax(-1000);


void citire()
{
	int x, y;
	cin >> n;
	v.push_back(0); // v[0] = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> x;
		v.push_back(x);
	}
	for (int i = 1; i < n; i++)
	{
		cin >> x >> y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
}

void DFS(int x, int s)
{
	smax = max(s, smax);
	viz[x] = 1;
	for (int i:a[x])
	{
		if (!viz[i])
		{
			DFS(i, s + v[i]);
		}
	}
	viz[x] = 0;
}
int main()
{
	citire();
	for (int i = 1; i <= n; i++)
	{
		DFS(i,v[i]);
	}
	cout << smax;
}