Pagini recente » Cod sursa (job #1980146) | Cod sursa (job #2106835) | Cod sursa (job #1762937) | Cod sursa (job #1329424) | Cod sursa (job #1452867)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int Inf = 0x3f3f3f3f;
int n, smax = -Inf;
vector<vector<int>> G;
vector<int> v, s, r;
vector<bool> b;
void Read();
int Dfs(int x);
int main()
{
Read();
Dfs(1);
fout << smax;
fin.close();
fout.close();
return 0;
}
void Read()
{
fin >> n;
G = vector<vector<int>>(n + 1, vector<int>());
s = vector<int>(n + 1, 0);
v = vector<int>(n + 1, 0);
r = vector<int>(n + 1, 0);
b = vector<bool>(n + 1, 0);
int x, y;
for ( int i = 1; i <= n; i++)
fin >> s[i];
while( fin >> x >> y )
{
G[x].push_back(y);
G[y].push_back(x);
}
}
int Dfs(int x )
{
int y = 0;
b[x] = true;
y += s[x];
for (const int& p : G[x])
if ( !b[p] )
{
y += Dfs(p);
}
// fout << x << ' ' << y << '\n';
if ( y > smax )
smax = y;
if ( y > 0 )
return y;
else return 0;
}