码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 274B 树形dp

时间:2015-02-18 16:25:40      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

//继续水一道树形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 }

 

CodeForces 274B 树形dp

标签:

原文地址:http://www.cnblogs.com/AC-Phoenix/p/4295800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!