题意:
给你一颗树,每个节点有一个v,一个w。
每次询问:i,T。
在节点i的子树中,恰好取∑v=T的一些节点,最大的w之和是多少?
可以说是非常牛逼的假题了。。假到现场竟然只有一人ac。。
由subtree[i]<=2/3subtree[fa[i]]可以得到,该树的高度不超过24(然后就懵逼了),然后就可以得知,所有节点的size和一定不超过20n(考虑单点贡献),所以可以直接用各种数据结构暴力维护所有子树节点
很容易想到用背包维护,但是复杂度真的太大了。。然而会发现一个很重要的信息就是,体积非常小,那么说明贪心策略是十分接近正解的,那可以先把性价比高的物品直接贪心地放下去,然后用背包把后几件物品去掉,再取接下来的几件物品做背包就可以了。。
可以说是十分乱搞了。。。
Backpack on Tree
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 213 Accepted Submission(s): 57
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
Sample Output
Hint
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.
Source
2016CCPC东北地区大学生程序设计竞赛 - 重现赛
Recommend
wange2014 | We have carefully selected several similar problems for you: 6447 6446 6445 6444 6443
Statistic | Submit | Discuss | Note