Topological sort

We must perform nn tasks, one at a time. Furthermore, some tasks must be done before others: there are mm precedence relations between tasks. Write a program to print a way to sort the nn tasks satisfying the mm given precedences.

Input

Input consists of several cases. Every case begins with nn, followed by mm, followed by mm distinct pairs xx yy that indicate that task xx must be done before task yy. You can assume 1n1041 \le n \le 10^4, 0m10n0 \le m \le 10n, and that the tasks are numbered from 0 to n1n - 1.

Output

For every case, print the lexicographically smallest order of the nn tasks that fulfills the mm given precedences. There will always be, at least, one solution.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T10:10:32.072Z

© Jutge.org, 2006–2026.
https://jutge.org