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
3
1 2
1 3
1 -1 1
output
3

题意:
给出一棵树,编号为1~n,点有权值(有小于0的)
你可以做以下操作:
选择树的一棵子树,但是这棵子树必须包含节点1,你可以把这棵子树的所有节点的权值都加1或者都减1
问:至少需要多少个操作,才可以把所有点权都变为0 明显,必须先让深度大的节点点权变为0,最后让根节点点权变为0
一次dfs就可以了
 #include<cstdio>
#include<cstring> using namespace std; #define ll long long
const int maxn=1e5+;
inline ll max(ll a,ll b)
{
return a>b?a:b;
} ll add[maxn]; //以i为根的子树需要做加法的次数
ll sub[maxn]; //以i为根的子树需要做减法的次数
//节点i为根的子树,做add[i]次加法和sub[i]次减法后,子树所有节点的点权都为0
ll w[maxn];
struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot=; void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void dfs(int ,int ); int main()
{
memset(head,-,sizeof head);
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
}
for(int i=;i<=n;i++)
{
scanf("%I64d",&w[i]);
}
dfs(,-);
printf("%I64d\n",add[]+sub[]);
return ;
} void dfs(int u,int pre)
{
add[u]=;
sub[u]=;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==pre)
continue;
dfs(v,u);
add[u]=max(add[u],add[v]);
sub[u]=max(sub[u],sub[v]);
}
w[u]=w[u]+add[u]-sub[u];
if(w[u]>=)
sub[u]+=w[u];
else
add[u]+=(-w[u]);
}

CF 274B Zero Tree 树形DP的更多相关文章

  1. 熟练剖分(tree) 树形DP

    熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...

  2. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  3. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

  4. CF 486D vailid set 树形DP

    As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are gi ...

  5. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  6. hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。

    /** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...

  7. 5.10 省选模拟赛 tree 树形dp 逆元

    LINK:tree 整场比赛看起来最不可做 确是最简单的题目. 感觉很难写 不过单独考虑某个点 容易想到树形dp的状态. 设f[x]表示以x为根的子树内有黑边的方案数. 白边方案只有一种所以不用记录. ...

  8. Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】

    题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...

  9. codeforces Round #263(div2) D. Appleman and Tree 树形dp

    题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...

随机推荐

  1. scala言语基础学习十二

  2. jQuery之Deferred对象的使用

    详见:http://www.imooc.com/code/8907 JavaScript的执行流程是分为"同步"与"异步" 传统的异步操作会在操作完成之后,使用 ...

  3. 【P1835】小红花

    很简单的题,然而我没想到,在NOIP上怎么办嘛QAQ 话说这题不知道怎么分类啊……先扔到玄学里边把…… 原题: Fj在圣诞节来临之际,决定给他的奶牛发一些小红花.现在Fj一共有N头奶牛,这N头牛按照编 ...

  4. WebForm 中的页面重定向和传值(转自 MSDN)

    ——原文地址:https://msdn.microsoft.com/zh-cn/library/6c3yckfw(v=vs.100).aspx      在开发 ASP.NET 网站时,您经常需要从一 ...

  5. Qt QTreeWidget节点的添加+双击响应+删除详解(转)

    QTreeWidget是实现树形结构的类,在很多软件中都可以看到类似树形结构的界面. 我做的一个示例如下图,用来处理图像,最顶层节点是图像的路径名,子节点是图像的各个波段,双击各个波段会显示图像各波段 ...

  6. <button>会自动提交表单吗?

    点击button以后,表单先由ajax提交,然后无论后台返回什么结果,页面都会跳转到表单action属性指定的路劲,也就是login.html使用的是html.jquery.javascript,后台 ...

  7. Python中strip()函数

    在python API中这样解释strip()函数:

  8. sql server ,OVER(PARTITION BY)函数用法,开窗函数,over子句,over开窗函数

    https://technet.microsoft.com/zh-cn/library/ms189461(v=sql.105).aspx https://social.msdn.microsoft.c ...

  9. 关于getClass().getClassLoader()

    关于getClass().getClassLoader()   InputStream   is   =   getClass().getClassLoader().getResourceAsStre ...

  10. es6新特性

    变量-------------------------- let, const:必须直接给一个变量赋值.注意,对象的属性或数组成员还是可以改变的. const MY_OBJECT = {some: 1 ...