6783.Longest Flight Route

Time Limit: 1s Memory Limit: 512MB

Uolevi has won a contest, and the prize is a free flight trip that can consist of one or more flights through cities. Of course, Uolevi wants to choose a trip that has as many cities as possible.

Uolevi wants to fly from Syrjälä to Lehmälä so that he visits the maximum number of cities. You are given the list of possible flights, and you know that there are no directed cycles in the flight network.

Input Format(From the terminal/stdin)

The first input line has two integers $n$ and $m$ : the number of cities and flights. The cities are numbered $1,2,\dots,n$ . City $1$ is Syrjälä, and city $n$ is Lehmälä.

After this, there are $m$ lines describing the flights. Each line has two integers $a$ and $b$ : there is a flight from city $a$ to city $b$ . Each flight is a one-way flight.

  • $2 \le n \le 10^5$
  • $1 \le m \le 2 \cdot 10^5$
  • $1 \le a,b \le n$

Output Format(To the terminal/stdout)

First print the maximum number of cities on the route. After this, print the cities in the order they will be visited. You can print any valid solution.

If there are no solutions, print "IMPOSSIBLE".

Sample Input

Copy
5 5
1 2
2 5
1 3
3 4
4 5
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output special judge

Copy
4
1 3 4 5
 \n
 · · · \n
Source: CSES, Graph Algorithms, 1680

Submit

请先 登录

© 2025 FAQs