#P6554. Promises I Can't Keep
Promises I Can't Keep
Background
I had so much certainty
Til that moment I lost control
And I've tried but it never was up to me
I've got no worse enemy
Than the fear of what's still unknown
And the time's come to realize there will be
Promises I can't keep
Problem Description
RFMC gives you a circuit and a power supply. He wants you to connect the power supply to some node of the circuit so that current can flow, and he promises to pay you an amount of money equal to the number shown on the circuit display.
This circuit has nodes. Each node has a weight . The nodes are connected by wires. You may connect the power supply to any starting node. Then the current starts flowing from that node. If the power supply is connected to a node , then next the current will, with equal probability and without visiting any node more than once, flow to a leaf node. The sum of the weights of all nodes that the current passes through is the number shown on the display (a leaf node means a node of degree excluding ).
Now you have choices of where to connect the power supply. You want the expected score to be as large as possible after connecting it, so you need to compute, within the time limit, the maximum among these expected values.
Input Format
The first line contains an integer , with the meaning as described above.
The next lines each contain two integers , indicating that there is a wire connecting nodes and .
The next line contains integers. The -th integer is , the weight of node .
Output Format
Output one floating-point number on one line, representing the maximum expected value.
This problem uses a special judge. Your answer will be accepted if its error from the correct answer does not exceed . The standard output keeps 4 decimal places.
5
1 2
1 5
2 3
2 4
2 3 1 -1 2
7.0000
Hint
Explanation for Sample 1:
When the power supply is connected to node 5, there are two possible paths: or . The scores are 8 and 6 respectively, so the expected value is 7. It can be proven that no other starting node gives an expected value greater than 7.
This problem uses bundled tests. Each score tier corresponds to one subtask.
For of the testdata, it is guaranteed that .
For another of the testdata, it is guaranteed that the graph is a chain.
For all testdata, it is guaranteed that .
The special judge code for this problem is already provided in the attachment.
Note: The input size is large, so you may use a faster input method. (The reference solution can pass when using scanf.)
Postscript: According to the title, RFMC will not keep his promises (just kidding).
The title is actually the name of a song.
Translated by ChatGPT 5