Topological orderings

There are nn tasks, which have to be done one by one. Some tasks must be done before others: there are mm precedence relations between tasks. Write a program that prints all possible ways to order the nn tasks according to the mm given precedences.

Input

Input consists of a natural number n1n \ge 1, followed by a natural number mm, followed by mm different pairs x,yx, y, indicating that task xx must be done before task yy. Suppose that the tasks are numbered from 0 to n1n - 1.

Output

Print, one per line and in lexicographic order, all the ways of sorting the nn tasks according to the mm given precedences. There will always be at least one solution.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T11:12:09.662Z

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