当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

5932:Backpack on Tree

题目描述
There is a rooted tree with n nodes. For each node i, there is an item whose volume is $c_i$ and value is $v_i$ and if node i is not the root, it is guaranteed that $|subtreei| \leq \frac{2}{3}|subtree_{father_i} |$.Bacon wants to pick items in $subtree_s$ so that their total volume is exactly t. Help Bacon determine the maximal total value of items he can pick.
输入解释
The first line contains one integer T($1 \leq T \leq 40$) and there are exactly T test cases below.

For each test case, the first line contains one integer n ($1 \leq n \leq 2 \times 10^4$).

The following n - 1 lines describe edges in the tree. Each line contains two integers  $a_i$ and $b_i (1 \leq a_i,b_i \leq n,a_i \neq b_i)$ describing an edge of the tree.

For the following n lines, the i-th line contains two integers $c_i$ and $v_i (1 \leq c_i \leq 5,1 \leq v_i \leq 10^9)$.

Next line contains one integer the number of queries Q and each of the following Q lines contains two integers $s_i$ and $t_i (1 \leq s_i \leq n, 1 \leq t_i \leq 10^5)$ as a query.

Note that node 1 is the root of the tree.

There is no more than 4 test cases that n is greater than $10^4$, and no more than 10  test cases that n is greater than $10^3$. sum of all Q are not greater than $2 \times 10^5$.
输出解释
For each test case, first line contains "Case #x:", where x indicates the number of test cases (starting from 1).

Then print Q lines and the i-th line contains the answer of the i-th query. Print -1 for the query if there is no way to pick items in $subtree_s$ with total volume t.
输入样例
2	
5	
1 2
1 3
1 4
1 5
1 1
2 2
3 3
4 4
5 5
3	
1 15
2 2
3 3
5	
1 2
1 3
1 4
4 5
5 123
3 4543
4 21
1 1231
2 12
3	
1 5
5 2
4 4
输出样例
Case #i:
15	
2	
3	
Case #2:
4555	
12	
-1	

提示
The tree in first case looks like the picture above,

For query subtree_s =1,t= 15,we should pick items in subtree 1. only method is to pick all
items in subtree 1 and get value 15.
来自杭电HDUOJ的附加信息
Recommend wange2014

该题目是Virtual Judge题目,来自 杭电HDUOJ

源链接: HDU-5932

最后修改于 2020-10-25T23:26:51+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
9000/4500MS(Java/Others) 65536/65536K(Java/Others)