White Collar
InterviewTime limit1sMemory limit1024 MB
Given a map of one-way roads, list every city that lies on some shortest path from city 1 to city N.
- Level
Medium4 of 10
- Topics
- Shortest path, BFS, Graph
- Solved
- No attempts yet
Problem
The top con artist in America, Neil Caffrey has pulled off every kind of theft and fraud there is. Tonight he plans to steal the Music Box, one of the finest works of art in the world.
This morning, AdbyMe, Inc. learned of the plan and is designing an operation to catch him. AdbyMe knows which city Neil is in now and which city holds the Music Box, and it will station agents to catch him. According to its intelligence, Neil is in a great hurry, so he will take the shortest path from his current location to the Music Box.
So AdbyMe plans to station agents in the city where Neil is now, the city with the Music Box, and every city he might pass through on the way. Look at the map and pick out every city where agents should be placed.
Input
The input consists of test cases. The first line contains .
The first line of each test case contains the number of cities () and the number of roads (). The next lines contain the numbers of the connected cities and . Every road is one-way, going from to . (, )
Neil is in city 1, and the Music Box is in city . There is always at least one path from city 1 to city .
Output
For each test case, print on its own line the numbers of the cities where AdbyMe, Inc. should station agents, in ascending order.