#P9248. [集训队互测 2018] 完美的集合
[集训队互测 2018] 完美的集合
Problem Description
Little A has a weighted tree with nodes. Each node has a weight and a value .
Now Little A wants to select some nodes to form a set , such that the total weight of these nodes is and they form a connected component. Little A is a perfectionist, so he will only choose those with the maximum possible total value among all sets that satisfy the conditions. We call such a set a perfect set.
Now Little A wants to choose sets from all perfect sets, and test these perfect sets separately. Before these tests begin, Little A first needs a node to place his testing device, whose maximum power is .
In each subsequent test, Little A will transmit energy once to all nodes in the test object . The power required to transmit energy to a node is , where denotes the length of the shortest path between nodes and on the tree. Therefore, if there exists a node in such that , the test will fail. At the same time, in order to ensure stable energy transmission, the node where the device is placed must be in the set , otherwise the test will also fail.
Now Little A wants to know: how many ways are there to choose sets from all perfect sets, such that he can find a node to place the testing device and complete his tests?
You only need to output the number of ways modulo .
Input Format
The first line contains four positive integers, denoting .
The next line contains positive integers, denoting .
The next line contains non-negative integers, denoting .
The next lines each contain three positive integers , meaning there is an edge of length connecting nodes in the tree.
Output Format
One number, denoting the number of ways modulo .
7 3 2 4
1 1 2 2 1 2 2
1 1 1 2 1 2 2
1 2 1
1 3 2
1 4 2
2 5 1
2 6 2
4 7 3
2
Hint
Sample Explanation
The perfect sets are .
The ways to choose sets from them and still complete the tests are: choosing , or choosing .
Constraints
| Subtask ID | Special Property | Score | |||
|---|---|---|---|---|---|
For of the testdata: , , , , and .
Translated by ChatGPT 5