Topological sort by prerequisites · Compiler

Topological sort by prerequisites

Schedule all n tasks. A directed edge from u to v requires u to finish before v. At each step complete the smallest-numbered unfinished task whose prerequisites are all complete.

Input
The first line contains n m. Vertices are numbered 0 through n-1 (1 <= n <= 50, 0 <= m <= 200). Each of the next m lines contains u v for an edge from u to v. Endpoints differ and each ordered pair occurs at most once.
Output
Print the task IDs in completion order on one space-separated line. If the tasks cannot all finish, print only IMPOSSIBLE.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…