Codeforces Round #168 (Div. 1) B. Zero Tree(树上DFS)
2017-08-03 21:42
435 查看
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
4000
-bit integers in С++. It is preferred to use the cin, cout streams
or the %I64d specifier.
Examples
input
output
题目大意:有一棵点有权值的一棵树,现在对任意一个联通的子树进行+1或者-1,但是必须每次修改包含1点,要求算出最小操作次数
解题思路:这道题对于子树的重新定义是这道题的关键,然后对于这棵树来说,我们可以固定1为根,我们每次从叶子节点进行操作,对于一个父亲节点来说,它存在着一个++或者--的操作次数,对于有多个子节点的父亲节点来说,每次对于++和--都同时取MAX,然后累加到该节点上,一直重复到1节点
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
4000
-bit integers in С++. It is preferred to use the cin, cout streams
or the %I64d specifier.
Examples
input
3 1 2 1 3 1 -1 1
output
3
题目大意:有一棵点有权值的一棵树,现在对任意一个联通的子树进行+1或者-1,但是必须每次修改包含1点,要求算出最小操作次数
解题思路:这道题对于子树的重新定义是这道题的关键,然后对于这棵树来说,我们可以固定1为根,我们每次从叶子节点进行操作,对于一个父亲节点来说,它存在着一个++或者--的操作次数,对于有多个子节点的父亲节点来说,每次对于++和--都同时取MAX,然后累加到该节点上,一直重复到1节点
相关文章推荐
- Codeforces Round #381 (Div. 2) D Alyona and a tree(DFS树上搞事)
- codeforces#428(div2)C Journey(树上的dfs水题)
- #树上DFS# Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons
- 【Codeforces Round 328 (Div 2)D】【树的直径 树的重心 贪心 两次dfs都找最小编号最远点】Super M 经过树上所有重要点的最小距离
- Codeforces Round #358 (Div. 2) -- C. Alyona and the Tree (树上的DFS)
- Codeforces Round #359 (Div. 2) C. Robbers' watch (DFS)
- Codeforces Round #316 (Div. 2) D. Tree Requests (DFS序)
- POJ 1816 Wild Words(trie 树上的DFS)
- HDU 5723 Abandoned country (并查集 + DFS+求解树上任意两点间的距离的平均值)
- 【Wunder Fund Round 2016 (Div 1 + Div 2 combined)D】【贪心 dfs】Hamiltonian Spanning Tree 树的最小链划分
- *Codeforces Round #316 (Div. 2)- D. Tree Requests (dfs+二分)
- Codeforces Round #428 (Div. 2) C. Journey(dfs deep
- C. Journey(dfs求期望+Codeforces Round #428 (Div. 2))
- 【Tyvj2133&BZOJ1146】网络管理Network(树套树,DFS序,树状数组,主席树,树上差分)
- Codeforces Round #294 (Div. 2) E 树上倍增lca
- DFS序——解决一类树上数据结构问题的方法
- Codeforces Round #316 (Div. 2) D 【dfs序+二分】
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job (dfs序 + 线段树)
- Codeforces Round #301 C (Div. 2) 【dfs】
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(dfs)