Pagini recente » Cod sursa (job #696115) | Cod sursa (job #2876747) | Cod sursa (job #76266) | Cod sursa (job #2193504) | Cod sursa (job #846473)
Cod sursa(job #846473)
#include <fstream>
#include <vector>
#include <stack>
#include <cstring>
#define INF 1<<30
using namespace std;
const char iname[] = "asmax.in";
const char oname[] = "asmax.out";
ifstream fin(iname);
ofstream fout(oname);
int N , M , i , j , x , y , comp_conexe , gasit , s1 , s2 , solc , sol;
int MN[ 16004 ];
int nr;
int cost_nod[ 16004 ];
bool viz[ 16004 ];
vector < int > Q[ 16004 ];
stack < int > st;
int main()
{
memset(MN,INF,sizeof(MN));
fin >> N;
for (i = 1; i <= N; ++i)
fin >> cost_nod[i];
for (i = 1; i <= N-1; ++i)
fin >> x >> y , Q[x].push_back(y) , Q[y].push_back(x);
vector < int > :: iterator it;
sol = (1<<30) * (-1);
for (i = 1; i <= N; ++i)
{
solc = MN[i] * (-1);
viz[i] = 1;
st.push(i);
s1 = s2 = 0;
while(!st.empty())
{
x = st.top();
gasit = 0;
while(!Q[x].empty())
{
if (!viz[Q[x].front()])
{
it = Q[x].begin();
++nr;
st.push(*it);
viz[*it] = 1;
s1 += cost_nod[*it];
if (MN[i] > s2)
MN[i] = s2;
if (solc < s1 - MN[i] && nr > 1)
solc = s1 - MN[i];
s2 += cost_nod[*it];
Q[x].erase(Q[x].begin());
gasit = 1;
break;
}
else
Q[x].erase(Q[x].begin());
}
if (!gasit)
st.pop();
}
if (solc > sol)
sol = solc;
memset(viz,0,sizeof(viz));
for (j = 1; j <= i; ++j)
viz[j] = 1;
}
fout << sol << '\n';
return 0;
}