#C5263. Task Execution Order

    ID: 48893 Type: Default 1000ms 256MiB

Task Execution Order

Task Execution Order

Given a set of tasks and a list of dependencies between them, determine a valid order to execute these tasks. Each dependency is provided as a pair \((a, b)\), meaning that task \(a\) must be executed before task \(b\). A valid order ensures that for every dependency, the prerequisite task is executed before the dependent task.

If no such order exists due to cyclic dependencies, output "Cyclic dependency detected".

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer \(n\), the number of tasks (numbered from 0 to \(n-1\)).
  • The second line contains an integer \(m\), the number of dependencies.
  • Each of the next \(m\) lines contains two integers \(a\) and \(b\) representing a dependency where task \(a\) must be executed before task \(b\).

outputFormat

If a valid task execution order exists, output the tasks in order separated by spaces on a single line.

If there is a cycle in the dependencies, output the string "Cyclic dependency detected".

## sample
3
0
0 1 2

</p>