#P10227. [COCI 2023/2024 #3] Slučajna Cesta
[COCI 2023/2024 #3] Slučajna Cesta
Background
Translated from COCI 2023/2024 Contest #3 T5 “Slučajna Cesta”.
Problem Description
Vito lives in a city with parks, numbered from to . The parks are connected by roads, and there is a path between every pair of parks. Each park has a beauty value; the beauty value of park is .
Last night, Vito decided to take a walk in the city. After he visits a park, he randomly chooses one road with equal probability and visits the park that the road leads to. However, before leaving, he looked out of a skyscraper window and saw that every road has either a blue snake or a red snake. Blue snakes attack everyone who travels from a park with a smaller number to a park with a larger number, while red snakes attack everyone who travels from a park with a larger number to a park with a smaller number. Since Vito does not want to be attacked by snakes, he decided to change his plan: when choosing a road at random, he only considers roads that he can pass safely without being attacked by snakes. Because he likes long walks, he will not stop as long as there is at least one road he can safely take.
When Vito went downstairs, he completely forgot which road had a red snake and which had a blue snake, so he wants to know: “If each road is equally likely to have a blue snake or a red snake, what is the expected beauty of my route if I start from park ?”
The beauty of a route is the sum of the beauty values of the parks visited along that route. The expected beauty of a route is defined as the sum, over all possible routes, of (the route beauty) multiplied by (the probability that Vito walks along that route).
Input Format
The first line contains an integer , the number of parks.
The second line contains integers , meaning there is a road between park and park .
The third line contains integers , where is the beauty value of park .
Output Format
On the -th line, output the expected beauty of Vito’s route starting from park . If this value is (where are integers), output , where is the modular inverse of modulo .
2
1
2 1
500000006
2
3
1 1
8 8 8
14
14
14
11
1 1 1 2 3 4 1 2 6 2
1 1000 5 3 18 200 8 9 0 2 2
968750272
610352580
450521029
536458466
199219275
662760680
190972315
90277951
824219264
941840425
532552597
Hint
Sample Explanation 1
The expected beauty of the route starting from the first park is $2.5\pmod{10^9+7}=\frac{5}{2}\pmod{10^9+7}=5\cdot 2^{-1}\pmod{10^9+7}=5\cdot 500000004\pmod{10^9+7}=500000006\pmod{10^9+7}$, and the expected beauty of the route starting from the second park is .
Sample Explanation 2
The probability that both snakes are red snakes is . In this case, if Vito starts from the first park, he will randomly choose which road to take.
Subtasks
| Subtask ID | Additional Constraints | Points |
|---|---|---|
| No value appears more than times in the sequence | ||
| No additional constraints |
Translated by ChatGPT 5