Codeforces 708C 树形DP


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: 131072/131072 K (Java/Others)

Problem Description

Bi Luo is a magic boy, he also has a migic tree, the tree has N nodes , in each node , there is a treasure, it's value is V[i], and for each edge, there is a cost C[i], which means every time you pass the edge i , you need to pay C[i].

You may attention that every V[i] can be taken only once, but for some C[i] , you may cost severial times.

Now, Bi Luo define ans[i] as the most value can Bi Luo gets if Bi Luo starts at node i.

Bi Luo is also an excited boy, now he wants to know every ans[i], can you help him?


First line is a positive integer T(T≤104) , represents there are T test cases.

Four each test:

The first line contain an integer N(N≤105).

The next line contains N integers V[i], which means the treasure’s value of node i(1≤V[i]≤104).

For the next N−1 lines, each contains three integers u,v,c , which means node u and node v are connected by an edge, it's cost is c(1≤c≤104).

You can assume that the sum of N will not exceed 106.


For the i-th test case , first output Case #i: in a single line , then output N lines , for the i-th line , output ans[i] in a single line.

Sample Input

4 1 7 7 7
1 2 6
1 3 1
2 4 8
3 5 2

Sample Output

Case #1:




2016中国大学生程序设计竞赛 - 网络选拔赛







在第二次DFS的时候,我们传入fa_back和fa_noback两个参数,分别代表从u点访问父亲节点后并且回到u点获得的最大收益和从u点访问父亲节点后不再访问u点获得最大价值。那么很显然,从u点出发,要么停留在u的子树中,要么停留在非u子树,两个值取max就好了,即max(subtree_max_back[u] + fa_noback, subtree_max_noback[u] + fa_back)。

那么在给u的v传的时候(这里用fb和fnb表示),应该传多少呢?fb很容易想到,就是subtree_max_back[u]减去访问v子树并返回的收益,再加上fa_back。对于fnb参数,就有点不好想了。假设v不是subtree_max_noback_idx[u],那么这个fnb就可能停留在非u子树,也可能停留在非v的u的子树中(这个值直接就是subtree_max_noback[u]减去访问v子树并返回的收益),那么,这种情况下,fnb=max(fa_noback + subtree_max_back[u] - max(0, subtree_max_back[v] - 2 * l),fa_back + subtree_max_noback[u] - max(0, subtree_max_back[v] - 2 * l));那么对于v=subtree_max_noback_idx[u],就不能选择停留在v子树了,除了可以选择停留在非u子树,还可以次大值subtree_submax_noback[u]的哦!和上面一样,扣除问v子树并返回的收益即可。那么这种情况下fnb = max(fa_noback + subtree_max_back[u] - max(0, subtree_max_back[v] - 2 * l),fa_back + subtree_submax_noback[u] - max(0, subtree_max_back[v] - 2 * l));



Codeforces 581F Zublicanes and Mumocrates 树形DP 分组背包

F. Zublicanes and Mumocrates
time limit per test

3 seconds

memory limit per test

512 megabytes


standard input


standard output

It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations on n main squares of the capital of Berland. Each of the n squares certainly can have demonstrations of only one party, otherwise it could lead to riots. On the other hand, both parties have applied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties.

Some pairs of squares are connected by (n - 1) bidirectional roads such that between any pair of squares there is a unique way to get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are called dead end squares.

The mayor of the capital instructed to distribute all the squares between the parties so that the dead end squares had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even.

To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number.


The first line of the input contains a single integer n (2 ≤ n ≤ 5000) — the number of squares in the capital of Berland.

Next n - 1 lines contain the pairs of integers x, y (1 ≤ x, y ≤ n, x ≠ y) — the numbers of the squares connected by the road. All squares are numbered with integers from 1 to n. It is guaranteed that the number of dead end squares of the city is even.


Print a single number — the minimum number of roads connecting the squares with demonstrations of different parties.

Sample test(s)