#Lutece1176. Game On the Tree
Game On the Tree
Migrated from Lutece 1176 Game On the Tree
All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.
Description
Given a tree, a connected graph that contains vertices and edges, you should control a virtual miner to get maximum values by walking from a vertex A and stopping at a vertex B.
On a tree, as we know, there is only one road between every two vertices. Here, you are allowed to choose a vertex A (the value of A can not be 0) and a vertex B by yourself. Walking from A and stopping at B, you must collect all the values on the road. Each vertex has a value. Try to get values as large as you can. Remember that the miner you controlled, can never go back to any vertex he has passed.
However, there is a special way to calculate total values. Let’s assume that the miner has passed vertices from A to B. During the travel, the miner has successively collected values worths . Vertex A has a value worth . The next vertex on the road has a value worth ...... At last, vertex B has a value worth . The special rule gives you an integer . The total value you collect is calculated by the formula .
It is guaranteed that are less than . The vertex A and B you choose can be same. But the value of A can not be 0. Output module . Note that you need to make sure as large as possible but make sure the remainder as large as possible. And then, output value of each vertex (stating from vertex A) on the road in the best case.
Input
The first line contains an integer , indicating the number of test cases.
For each case, The first and second line contain two integers and , indicating the number of vertices and the integer .
Each of the following lines contains two integers and , indicating that there is an edge connecting vertex and vertex .
The following line contains integers , the value of each vertex. It is guaranteed that at least one of not equal 0.
You can assume that sum of does not exceed .
Output
For each case, the first line outputs "Case #: "(without quotes). Here, is the index of test case (starting from 1) and is the maximum value the miner can collect module .
The second line outputs the value of each vertex from vertex A to vertex B.
Samples
2
8
2
1 2
2 3
3 4
4 5
2 6
6 7
7 8
1 0 0 0 0 0 0 0
9
1000000000
1 2
2 3
1 4
4 5
1 6
6 7
1 8
8 9
1 2 0 2 0 2 0 2 0
Case #1: 16
1 0 0 0 0
Case #2: 999999356
2 1 2 0
Resources
peterpan