The Tree of Life
In the Forest of X, God created the tree of life.
He gives each node (also called a node) of each tree an integer that represents the harmony value of the point.
God will choose a non-empty node set in this tree, so that for any two points in S a,b, there is a point column {A, V1, v2, ..., VK, b} so that each point in the point column is an element of s, and there is an edge between the two adjacent points in the sequence.
In this context, God wants to make the points in s that correspond to the integers and as large as possible.
This is the greatest and the score that God gives to the tree of life.
After an ATM effort, he had already known the whole number of gods given to each node on each tree. But because ATM is not good at computing, he doesn't know how to score effectively. He needs you to write a program for him to calculate the score of a tree.
"Input Format"
The first line of an integer n means that the tree has n nodes.
The second row n integers, which in turn represents the score for each node.
Next n-1 line, 2 integers per line u, V, indicating the existence of a U to v side. Since this is a tree, there is no ring.
"Output Format"
Output a line of numbers, which means God gives the tree a score.
"Sample Input"
5
1-2-3 4 5
4 2
3 1
1 2
2 5
"Sample Output"
8
"Data Range"
For 30% of the data, N <= 10
For 100% of the data, 0 < n <= 10^5, the absolute value of each node's score does not exceed 10^6.
Resource conventions:
Peak memory Consumption < 256M
CPU Consumption < 3000ms
Please output strictly according to the requirements, do not superfluous print similar: "Please enter ..." Superfluous content.
All the code is placed in the same source file, after debugging, the copy is submitted to the source.
Note: The main function needs to return 0
Note: Use only the ANSI C/ansi C + + standard and do not invoke special functions that depend on the compilation environment or the operating system.
Note: All dependent functions must be explicitly #include <xxx> in the source file, and the common header files cannot be omitted from the project settings.
When submitting, be careful to select the type of compiler you expect.
#include "iostream"
#include "vector"
#define MINN-2000000000
#define MAXX 100005
using namespace std ;
Dp[i] A rating of the Tree of life with I as the root (
int rst=minn,v[maxx],dp[maxx],vis[maxx];
Vector<int> E[maxx];
void Dfs (int s)
{
vis[s]=true;
Dp[s]=v[s];
for (int i=0;i<e[s].size (); i++)
{
if (!vis[e[s][i]])
{
dfs (e[s][i));
if (dp[e[s][i]]>0)
Dp[s]+=dp[e[s][i]];
}
Rst=max (Dp[s],rst);
}
int main ()
{
int n,u,v;
cin>>n;
for (int i=1;i<=n;i++)
cin>>v[i];
for (int i=1;i<n;i++)
{
cin>>u>>v;
E[u].push_back (v);
E[v].push_back (u);
}
DFS (1);
cout<<rst;
}