HIT2954PD EERT题解动态规划DP

http://acm.hit.edu.cn/judge/show.php?Proid=2954&Contestid=0

PD EERT

 


Time limit:1sec.Submitted:82
Memory limit:64MAccepted:35
Source: PerfectCai&Xnozero

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的子树的最大值

 

代码:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值