Driving Out the Piggies
Time limit1sMemory limit128 MB
A bomb starts at city 1 of an undirected graph, detonates at each visit with probability P/Q and otherwise moves to a random neighbor; find the detonation probability for every city.
- Level
Medium6 of 10
- Topics
- Probability, Graph, Dynamic programming, Math
- Solved
- No attempts yet
Problem
The Cows have built a randomized stink bomb to drive the Piggies out of their land. The Piggy civilization has cities, numbered through , connected by bidirectional roads. Each road joins two different cities, and city is guaranteed to be connected to at least one other city.
The stink bomb is placed in city . Every hour — including the very first one — the bomb sits in some city and does exactly one of two things:
- With probability it detonates, polluting the city it currently occupies, and the process stops.
- Otherwise (with probability ) it does not detonate. It then chooses one of the roads leaving its current city uniformly at random and travels along it to the neighboring city. Every road leaving a city is equally likely to be chosen.
Because the bomb wanders at random, the Cows want to know, for every city, the probability that the bomb eventually detonates there (polluting it).
For example, suppose there are two cities joined by a single road, and the bomb — starting in city — detonates with probability each time it enters a city:
1--2
Writing each possible journey as the sequence of cities the bomb visits (it detonates in the last city listed), the journeys are:
1
1-2
1-2-1
1-2-1-2
1-2-1-2-1
...
The bomb detonates in city exactly on the journeys that pass through an odd number of cities (1; 1-2-1; 1-2-1-2-1; and so on). A journey through cities occurs with probability : the bomb must fail to detonate in each of the first cities (probability each) and then detonate in the -th (probability ). Summing the odd-numbered terms gives the probability of detonating in city :
so the probability of detonating in city is .
Input
- Line 1: four space-separated integers , , , and (; ; ; ; ).
- Lines 2 to : each line contains two space-separated integers and (; ; ), describing a bidirectional road between cities and .
Output
Print lines. On line , print the probability that city is polluted, written with exactly digits after the decimal point.