Topological sort

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

Input

Input consists of several cases. Every case begins with n, followed by
m, followed by m distinct pairs x y that indicate that task x must be
done before task y. You can assume 1 ≤ n ≤ 10⁴, 0 ≤ m ≤ 10n, and that
the tasks are numbered from 0 to n − 1.

Output

For every case, print the lexicographically smallest order of the n
tasks that fulfills the m 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
