编程介的小学生 2018-12-03 14:41 采纳率: 20.5%
浏览 491
已采纳

一句话没办法说的很清楚,所以请看下面的问题描述

Problem Description
There is a rooted tree with n nodes. For each node i, there is an item whose volume is ci and value is vi and if node i is not the root, it is guaranteed that |subtreei|≤23|subtreefatheri|.Bacon wants to pick items in subtrees so that their total volume is exactly t. Help Bacon determine the maximal total value of items he can pick.

Input
The first line contains one integer T(1≤T≤40) and there are exactly T test cases below.

For each test case, the first line contains one integer n (1≤n≤2×104).

The following n - 1 lines describe edges in the tree. Each line contains two integers ai and bi(1≤ai,bi≤n,ai≠bi) describing an edge of the tree.

For the following n lines, the i-th line contains two integers ci and vi(1≤ci≤5,1≤vi≤109).

Next line contains one integer the number of queries Q and each of the following Q lines contains two integers si and ti(1≤si≤n,1≤ti≤105) 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 104, and no more than 10 test cases that n is greater than 103. sum of all Q are not greater than 2×105.

Output
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 subtrees with total volume t.

Sample Input
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

Sample Output
Case #i:
15

2

3

Case #2:
4555

12

-1

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-03-28 10:37
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作