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

你可能感兴趣的:(CodeForces 274B 树形dp)