http://acm.hit.edu.cn/judge/show.php?Proid=2954&Contestid=0
PD EERT
Time limit: | 1sec. | Submitted: | 82 |
Memory limit: | 64M | Accepted: | 35 |
Description Maybe you have learned the data structure -- Tree. Now there is a tree, which has n (1 ≤ n ≤ 1000) nodes(numbered from 1 to n), each node has a value c (-100 ≤ c ≤ 100). Can you find a subtree of this tree, which has the maximum value. InputThere will be several test cases. The first line of each case will contain a single positive integer n giving the number of nodes. The second line will contain n integers, giving the value of each node(from node 1 to node n). Then followed n-1 lines, each line has two numbers a and b, means there is an edge between a and b. OutputFor each case, output one integer, the maximum value. Sample Input 5 -2 1 1 1 1 1 2 1 3 1 4 1 5 Sample Output2 |
题目倒过来是TREE DP
很简单的树形dp
状态:
d[i]第i个结点的子树最大值
状态转移方程:
d[i]=max(0,a[i])叶子结点
d[i]=max(0,a[i],a[i]+dfs(i))
dfs(i)表示i的子树的最大值
代码: