Graph Theory37 sections · 1633 units
Open in Course

Problem - Longest Flight Route

The code

You have nn cities and mm flight routes. Each route is a one-way flight from city aa to city bb.

Find a route from city 11 to city nn that visits the maximum number of cities. Print the maximum number of cities on the route, followed by the cities in order.

If there is no route from 11 to nn, print "IMPOSSIBLE".

Example:

  • Input: n=5n = 5, flights: 121 \to 2, 232 \to 3, 343 \to 4, 141 \to 4, 454 \to 5
  • Output: 55 and route 123451 \to 2 \to 3 \to 4 \to 5 (visits all 55 cities)