Algorithms – Le Tour de Sofia (Solution)

$ 24.99

Description

Pierre is training for the famous bicycle tour of Sofia. He is a cyclist, the laziest at that, and he is planning a workout route…the shortest one.
You are given all junctions and all one-way streets in Sofia. Help Pierre find the length of the shortest route starting at a given junction and ending back there. If there is no such route print the numbers of junctions reachable from the start.
Input
• The first line holds an integer n – the number of junctions
• On the second line, you will receive the number m – the number of streets
• On the third line, s – the start of the route
• At the next m lines, you will receive the streets in the format: {from junction} {to junction}
Output
• If there is a route from the start and back to it, print the length of the route
• If there is no such a route, print the count of reachable junctions from the start
Constraints
• Number of junctions will be an integer in the range [0…10000] • Number of streets will be an integer in the range [0…10000]
• All junctions will be numbered from 0 to N – 1.
• Time limit: 100 ms. Allowed memory: 20 MB
Examples
Input Output Visual Comment
7
10
0
0 2
2 1
1 0
3 0
3 1
2 4
3 4
4 5
5 6
6 3 3 The shortest route starting at 0 is:

0 → 2 → 1 → 0

It has length of 3
3
2
1
0 1
1 2 2 There is no route for Pierre. Junctions that can be reached: 1, 2

Reviews

There are no reviews yet.

Be the first to review “Algorithms – Le Tour de Sofia (Solution)”

Your email address will not be published. Required fields are marked *