CodeForces - 274B Zero Tree — 树形DP


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.

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 integersv1, 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 cincout streams or the %I64d specifier.

Example
Input
3
1 2
1 3
1 -1 1
Output
3


题目大意:给你一个n。给你n-1条边使得这1-n这n个点连成一个树形的结构。


再给你n个数,是这n个点分别代表的权值。。


每次的操作是在整棵树中挑出一棵以1为根结点的子树。然后将所有在这棵子树中的点的权值 + 1  or - 1;

问最少需要几步可以将所有点的权值都变为0。



#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <sstream>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a));
#define For(a,b) for(int i = a;i<b;i++)
#define ll long long
#define MAX_N 100010

using namespace std;

ll x[MAX_N],d[MAX_N],u[MAX_N];
vector<int> e[MAX_N];

void dfs(int v,int y)
{
    u[v] = 0;
    d[v] = 0;
    for(int i = 0; i<e[v].size(); i++)
    {
        if(e[v][i] != y)
        {
            dfs(e[v][i],v);
            u[v] = max(u[v],u[e[v][i]]);
            d[v] = max(d[v],d[e[v][i]]);
        }
    }
    x[v] += (u[v] - d[v]);
    if(x[v]>0) d[v] += x[v];
    else u[v] -= x[v];
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 0; i<n-1; i++)
        {
            int v,y;
            scanf("%d %d",&v,&y);
            e[v].push_back(y);
            e[y].push_back(v);
        }
        for(int i = 1; i<=n; i++)
            scanf("%lld",&x[i]);
        dfs(1,-1);
        printf("%lld\n",d[1] + u[1]);
        for(int i = 0; i<MAX_N; i++) e[i].clear();
    }
    return 0;
}






















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值