poj 2342 树形dp

//再水一发树形dp

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 using namespace std;
 6 int dp[6010][2];
 7 bool vis[6010];
 8 int tot, first[6010], next[6010], to[6010];
 9 int n, ans[6010];
10 void dfs(int root)
11 {
12     vis[root] = 1;
13     dp[root][1] = ans[root];
14     int edge;
15     for(edge = first[root]; edge; edge = next[edge]) {
16         dfs(to[edge]);
17         dp[root][0] += max(dp[to[edge]][0], dp[to[edge]][1]);
18         dp[root][1] += dp[to[edge]][0];
19     }
20 }
21 
22 int main()
23 {
24     int i;
25     scanf("%d", &n);
26     for(i = 1; i <= n; ++i)
27         scanf("%d", &ans[i]);
28     int b, a;
29     tot = 0;
30     int root = n * (n + 1);
31     root >>= 1;
32     while(scanf("%d%d", &b, &a) && (a || b)) {
33         next[++tot] = first[a];
34         first[a] = tot;
35         to[tot] = b;
36         root -= b;
37     }
38     dfs(root);
39     printf("%d\n", max(dp[root][0], dp[root][1]));
40 }

 

转载于:https://www.cnblogs.com/AC-Phoenix/p/4295738.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值