Lost in the Night
InterviewTime limit2sMemory limit512 MB
On a tree, a walker starting at node A repeatedly picks a uniformly random neighbor until reaching hotel B or C; find the probability of hitting B first.
- Level
Medium6 of 10
- Topics
- Probability, Graph, Tree, Math
- Solved
- No attempts yet
Problem
A city in Nlogonia has roundabouts and streets. Each street joins two different roundabouts, and the streets alone let you travel from any roundabout to any other one.
The city has only two hotels. The cheap one is on roundabout and the expensive one is on roundabout . A tourist came to the city for a friend's birthday party, held at a club on roundabout . He booked neither hotel, and the night is pleasant, so after the party he decided to walk the streets and roundabouts until he runs into a hotel. He will stay at the first hotel he finds.
The plan turned out harder than he expected. He does not know the city and he drank more than he should have, so every street looks the same to him. He settled on this strategy. At each roundabout he picks one of the streets leaving it, all with the same probability, and takes that street to another roundabout. He stops as soon as he reaches a roundabout that has a hotel on it. Because he cannot tell the streets apart, he sometimes picks the street he just walked in on.
Given the street layout, the location of the party, the location of the cheap hotel and the location of the expensive hotel, write a program that computes the probability that the tourist reaches the cheap hotel before the expensive one.
Input
The input holds several test cases and ends at end of file. The first line of a test case has four integers , , and : the number of roundabouts, the roundabout where the birthday party is held, the roundabout with the cheap hotel, and the roundabout with the expensive hotel. Each of the next lines has two integers and , meaning a street joins roundabouts and .
Constraints
- The input has at most 100 test cases.
- , ,
- The streets always connect every roundabout.
Output
For each test case print one line with the probability that the tourist reaches the cheap hotel before the expensive one, rounded to six decimal places. Print all six digits, so a probability of 0 is 0.000000 and a probability of 1 is 1.000000.