您的位置:首页 > 其它

【codeforces 274B】【树形DP】 B. Zero Tree【一棵树,每个点有权值,每次操作可以对一个联通子集中的点全部加或者减1,且每次操作必须包含点1,问最少多少次操作权值全为0】

2016-10-05 14:26 881 查看
传送门:B. Zero Tree

描述:

B. 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:

Select the subtree of the given tree that includes the vertex with number 1.

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 ≤ n; ai ≠ 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 %I64dspecifier.

Examples

input
3
1 2
1 3
1 -1 1


output
3


题意:
给出一棵树,每个点有权值,每次操作可以对一个联通子集中的点全部加1,或者全部减1,且每次操作必须包含点1,问最少通过多少次操作可以让整棵树每个点的权值变为0.

题目定义的subtree,不过这个subtree和子树的概念还是有些不同的

思路:

定义状态up[u],down[u]代表点u被加操作的次数和点u被减操作的次数因为必须包含点1,所以我们将树的根定在点1,那么对于每一点的子树中点,如果要修改的话,那么一定会经过当前这个点,因为这是通向根的必经之路,所以把1设为根。所以对于每个点u,它被加修改和减修改的次数,就是它的儿子中进行该操作的最大次数,因为如果有两个儿子都需要进行该操作,那么完全可以两步并一步,所以只需要取最大值就可以了。那么也就是
up[u]=maxv adjacent to uup[v]
down[u]同理。因为每次修改一定会修改点1,所以最后答案就是up[1]+down[1] 

代码:
#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << " " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using namespace std;

template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}

const int N=1e5+10;

ll val
,down
,up
;
std::vector<int> e
;
int n;

void dfs(int u,int fa){
up[u]=down[u]=0;
for(int i=0; i<e[u].size(); i++){
int v=e[u][i];
if(v==fa)continue;
dfs(v, u);
up[u]=max(up[u], up[v]);
down[u]=max(down[u], down[v]);
}
val[u]+=up[u]-down[u];
if(val[u]>0)down[u]+=val[u];
else up[u]-=val[u];
}

int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif

read(n);
for(int i=1; i<n; i++){
int x,y;
read(x);read(y);
e[x].push_back(y);
e[y].push_back(x);
}
for(int i=1; i<=n; i++)read(val[i]);
dfs(1, 0);
print(up[1]+down[1]);
return 0;
}

/*input
5
2 3
4 5
2 5
1 3
0 2 1 4 3
output
8*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐