#P4802. Longest Path
Longest Path
Longest Path
This problem is adapted from CCO 2015 Day1 T2, Artskjid.
Troy loves to travel leisurely during his vacations. Instead of finding the shortest route via GPS, he wants to take the longest possible route from his starting city to his destination city so that he can discover many new and interesting places.
A valid path is a sequence of distinct cities such that for each , there exists a road from to .
Your task is to find the longest such path. If no valid path exists, output "IMPOSSIBLE".
inputFormat
The first line contains two integers and , representing the number of cities and the number of roads respectively.
Each of the next lines contains two integers and , indicating a directed road from city to city .
outputFormat
If there is no valid path from city to city , output a single line with the text "IMPOSSIBLE".
Otherwise, the first line should contain an integer representing the number of cities in the longest path, and the second line should list the cities in order, separated by spaces.
sample
5 5
1 2
2 3
3 5
1 4
4 5
4
1 2 3 5
</p>