CodeForces 274B 树形dp

//继续水一道树形dp

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 #include "cmath"
 6 using namespace std;
 7 __int64 dp[100010][2];
 8 bool vis[100010];
 9 int tot, first[100010 << 1], next[100010 << 1], to[100010 << 1];
10 int n, ans[100010];
11 void dfs(int root)
12 {
13     vis[root] = 1;
14     int edge, son;
15     for(edge = first[root]; edge; edge = next[edge]) {
16         son = to[edge];
17         if(!vis[son]) {
18             dfs(son);
19             dp[root][0] = max(dp[root][0], dp[son][0]);
20             dp[root][1] = max(dp[root][1], dp[son][1]);
21         }
22     }
23     __int64 rem = ans[root] + dp[root][0] - dp[root][1];
24     if(rem < 0)
25         dp[root][0] -= rem;
26     else
27         dp[root][1] += rem;
28 }
29 
30 int main()
31 {
32     int i;
33     scanf("%d", &n);
34     int a, b;
35     for(i = 1; i <= n - 1; ++i) {
36         scanf("%d%d", &a, &b);
37         next[++tot] = first[a];
38         first[a] = tot;
39         to[tot] = b;
40         next[++tot] = first[b];
41         first[b] = tot;
42         to[tot] = a;
43     }
44     for(i = 1; i <= n; ++i) {
45         scanf("%d", &ans[i]);
46     }
47     dfs(1);
48     printf("%I64d\n", dp[1][0] + dp[1][1]);
49 }

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值