D. Zero Tree
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices.

A subtree of a tree T is a tree with both vertices and edges as subsets of vertices and edges of T.

You're given a tree with n vertices. Consider its vertices numbered with integers from 1 to n. Additionally an integer is written on every vertex of this tree. Initially the integer written on the i-th vertex is equal to vi. In one move you can apply the following operation:

  1. Select the subtree of the given tree that includes the vertex with number 1.
  2. Increase (or decrease) by one all the integers which are written on the vertices of that subtree.

Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero.

Input

The first line of the input contains n (1 ≤ n ≤ 105). Each of the next n - 1 lines contains two integers ai and bi (1 ≤ ai, bi ≤ nai ≠ bi) indicating there's an edge between vertices ai and bi. It's guaranteed that the input graph is a tree.

The last line of the input contains a list of n space-separated integers v1, v2, ..., vn (|vi| ≤ 109).

Output

Print the minimum number of operations needed to solve the task.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Sample test(s)
Input
31 21 31 -1 1

Output
3

一开始题目理解错误:“includes the vertex with number 1.”是指结点1而非其上integer为1的结点。思路是先处理儿子再处理父亲。这是因为:设有结点x,x的val变为0所需操作次数x.count = in + de + r,这里in是x因为儿子结点的+1操作而随之产生的+1操作的次数(对儿子的操作会传递到父亲),而de就是-1操作,显然,in是x的所有儿子中+1次数的最大值,de是所有儿子中-1次数的最大值。而r是什么?在处理完儿子后确保儿子的val为0,但不保证x的val为0,r就是余下的对x操作的次数:r=x.val+in-de(这些操作是发生在x和他的父亲之间的)。由以上于可见,对x.count的统计能转化为对其儿子结点的相关信息的统计,故而此前说“先处理儿子后处理父亲”。结点1是最后处理的。这里还涉及如何保存一个结点的所有儿子的信息的问题,我开始用邻接表,超时,后来参考别人的代码才想起来可以用vector。

AC Code:
 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <map>
 5 #include <vector>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <cstdio>
10 #include <cstring>
11 #include <algorithm>
12 using namespace std;
13 #define LL long long
14 #define cti const int
15 #define dg(i) cout << "*" << i << endl;
16
17 cti MAXN = 100005;
18 int n;
19 bool vis[MAXN];
20 LL v[MAXN];
21 struct Count
22 {
23     LL in;  //结点i增1次数
24     LL de;  //结点i减1次数
25 }cnt[MAXN];
26 vector<LL> node[MAXN];
27
28 Count DP(int i)
29 {
30     vis[i] = true;
31     LL in = 0, de = 0;  //in for increse, de for decrese
32     Count t;
33     for(vector<LL>::iterator it = node[i].begin(); it != node[i].end(); it++)
34     {
35         if(vis[*it]) continue;
36         t = DP(*it);
37         if(t.in > in) in = t.in;
38         if(t.de > de) de = t.de;
39     }
40     cnt[i].de = de;
41     cnt[i].in = in;
42     int d = v[i] - cnt[i].de + cnt[i].in;
43     if(d > 0) cnt[i].de += d;
44     else cnt[i].in -= d;
45     return cnt[i];
46 }
47
48
49 int main()
50 {
51     while(scanf("%d", &n) != EOF)
52     {
53         for(int i = 1; i <= n; i++)
54         {
55             vis[0] = false;
56             while(!node[i].empty()) node[i].pop_back();
57         }
58         int a, b;
59         for(int i = 1; i < n; i++)
60         {
61             scanf("%d %d", &a, &b);
62             node[a].push_back(b);
63             node[b].push_back(a);
64         }
65         for(int i = 1; i <= n; i++)
66             scanf("%I64d", &v[i]);
67         Count ans = DP(1);
68         printf("%I64d\n", ans.de + ans.in);
69     }
70     return 0;
71 }

Codeforces Round #168 (Div. 2)D. Zero Tree(DP,中等难度)相关推荐

  1. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

  2. Codeforces Round #743 (Div. 2) E. Paint 区间dp + 暴力

    传送门 文章目录 题意: 思路: 题意: 给你一个有nnn个像素的图像,每个像素都有一个颜色aia_iai​,保证每种颜色的图像不会超过202020个.你现在每次可以选择一个颜色,并选择一段连续的像素 ...

  3. Codeforces Round #709 (Div. 1) C. Skyline Photo dp + 单调栈优化

    传送门 文章目录 题意: 思路: 题意: 思路: 首先一个非常明显的dpdpdp式子就是f[i]=max(f[j]+val(j+1,i))f[i]=max(f[j]+val(j+1,i))f[i]=m ...

  4. Codeforces Round #627 (Div. 3) E. Sleeping Schedule dp

    传送门 文章目录 题意: 思路: 题意: 给你一天hhh小时,初始时间是000,每天可以使时间+ai+a_i+ai​或者+ai−1+a_i-1+ai​−1,问最多可以让多少天的时间在[l,r][l,r ...

  5. Codeforces Round #717 (Div. 2) D(倍增dp)

    Codeforces Round #717 (Div. 2) D 题意:n个数 q个询问,每一个询问有l和r,问你l到r这段区间中最少能分成几段,每一段中的数都是互质的. 思路:首先预处理出每一个点向 ...

  6. Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

  7. Codeforces Round #665 (Div. 2) Maximum Distributed Tree(树上贪心)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 CF1401D Maximum Distributed Tree(树上贪心) 给定一棵 nnn 个节点 ...

  8. Codeforces Round #168 (Div. 2)

    这场比赛因为坐火车没有做,到学校了, 坑爹的学校晚上要熄灯. 还有一些原因以后cf就不能及时的做了. 但是每场我肯定都不会落下. 这场b题严重坑, 被坑了1个半小时,关键还是自己的心理在作祟,思维不行 ...

  9. 【Codeforces Round #544 (Div. 3) F2. Spanning Tree with One Fixed Degree】DFS

    F2. Spanning Tree with One Fixed Degree 题意 给你nnn个点mmm条边的无向联通图,找出一棵生成树,使111这个点的度=d=d=d. 1≤n,m≤1051 \l ...

最新文章

  1. 独家 | 一文读懂人工神经网络
  2. 使用Laya引擎开发微信小游戏(下)
  3. 一些日常工具集合(C++代码片段)
  4. ruby格式化SQL语句
  5. JQuery Ajax传递整个表单数据方法
  6. 我搜集的C++字符类型的相互转换
  7. WPF 2D绘图(2)Geometry
  8. .net大型分布式电子商务架构说明(转载来自头条)
  9. 如何将常规元组或字典转换为 namedtuple
  10. eurekaAutoServiceRegistration 异常
  11. 谁能教教我, 这个插件是怎么破解 yunfile, yifile, ctfile, 77file 等网盘的
  12. android开源系统brvah,BRVAH(让RecyclerView变得更高效)(1)
  13. 敏捷模式下携程的接口自动化平台演变
  14. 数据库系统概述--关系数据库标准语言SQL
  15. 常见的安全产品与服务整理
  16. 麻将与扑克的文化内涵
  17. 流放者柯南自建服务器 linux,《流放者柯南》自建服务器教程一览 服务器搭建方法介绍...
  18. 阿里云对运营10多年来持续最久的故障发布复盘说明
  19. 微信v3支付【php】
  20. 区块链存在哪些安全缺陷?怎么解决?

热门文章

  1. iOS 获取app进程被杀死事件applicationWillTerminate
  2. curl命令的超时时间
  3. Hinton神经网络公开课编程练习1 The perceptron learning algorithm
  4. USACO 1.3... 虫洞 解题报告(搜索+强大剪枝+模拟)
  5. bmob php支付,基于Bmob在小程序端实现一键支付
  6. c++ 字典_python字典详解-超级完整版
  7. 【转载】通俗理解极大似然估计
  8. Windows Azure Cloud Service (27) 在Windows Azure发送邮件(上)
  9. 让你眼花缭乱的JS代码~~
  10. Win10 Anaconda下TensorFlow-GPU环境搭建详细教程(包含CUDA+cuDNN安装过程)(转载)...