Pagini recente » Cod sursa (job #350916) | Cod sursa (job #2951564) | Cod sursa (job #596686) | Cod sursa (job #209573) | Cod sursa (job #528303)
Cod sursa(job #528303)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const char iname[] = "asmax.in";
const char oname[] = "asmax.out";
const int nmax = 16384;
ifstream fin(iname);
ofstream fout(oname);
int dp[nmax];
vector<int> Gr[nmax];
int i, j, k, n, m, x, y, rad, maxk;
int Cost[nmax], incoming[nmax], outcoming[nmax], tata[nmax], viz[nmax];
void DF(int nod)
{
viz[nod] = 1;
if(outcoming[nod] == 0)
dp[nod] = Cost[nod];
for(vector<int>::iterator it = Gr[nod].begin(); it != Gr[nod].end(); ++it)
{
if(viz[*it] == 0)
{
viz[*it] = 1;
DF(*it);
if(Cost[nod] < Cost[nod] + Cost[*it])
Cost[nod] += Cost[*it];
}
}
if(Cost[nod] > maxk)
maxk = Cost[nod];
}
int main()
{
fin >> n;
for(i = 1; i <= n; i ++)
{
fin >> Cost[i];
dp[i] = Cost[i];
}
for(i = 1; i <= n - 1; i ++)
{
fin >> x >> y;
Gr[x].push_back(y);
incoming[y]++;
outcoming[x]++;
tata[y] = x;
}
for(i = 1; i <= n; i ++)
if(incoming[i] == 0)
{
rad = i;
break;
}
DF(rad);
fout << maxk << "\n";
return 0;
}