LIM - Lost in Madrid

Lost in Madrid

Programming contests can be very exhausting. After five hours of intensive programming, you want to get some well-deserved rest and make yourself on the way to your hotel. Unfortunately, you don't quite remember the way to get there... but that doesn't matter: In good spirits (due to a successful contest?) you set out.

As you don't know the exact way, you decide to walk around in the following fashion: Start at the contest site (denoted by id 0) and choose a street at random. Follow the street to the next intersection, and choose another street at random. Every street at an intersection has the same probability of being chosen. You might even decide to take the street back where you came from. As you're on foot, you can use the streets in both directions, unlike in "Madrid's One Way Streets".

Your walk stops once you encounter your hotel (id = 300) or one of the tourist information booths (id > 290) where you can ask for the way. You can assume there is at least one path connecting you to either type of object.

Because you don't speak a lot of spanish (apart from some verbs that you can conjugate thanks to problem "Spanish Verbs"), you'd like to know the probability that you arrive at your hotel directly, without first arriving at a tourist information booth.


The input consists of several testcases, separated by an empty line.

Each testcase starts with S, the number of streets. The following S lines contain two numbers 0 ≤ A, B ≤ 300 each. This means that there is a street connecting intersection A to intersection B. The same street will not appear multiple times in the input.

The input ends with S=0. This testcase should not be processed.


For each testcase, print the probability to arrive directly at the hotel, rounded to three decimal places.


0 291
0 292
0 300

0 300
291 300

0 291
291 300

0 292
0 88
0 14
0 300
292 88
88 300
14 300



hide comments
Srijan Khare: 2014-02-08 10:53:14

nice question :)

Tony Beta Lambda: 2009-11-16 07:09:15

Be careful with cases that lead to the answer of -0.000.

Last edit: 2009-11-16 07:38:57
SALVO: 2009-11-02 07:59:21

Can any one explain how Probability for the last case is 0.579 .. i get 0.673 ??

Added by:Jonas Wagner
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 C++ 4.3.2 ERL NODEJS OBJC PERL6 SQLITE VB.NET