#C7024. Determining a Valid Task Order

    ID: 50850 Type: Default 1000ms 256MiB

Determining a Valid Task Order

Determining a Valid Task Order

You are given (N) tasks numbered from 0 to (N-1) and (M) dependency relations. Each dependency is given as a pair of integers (u) and (v) indicating that task (u) must be performed before task (v). The goal is to determine a valid order to perform all tasks. If there exists a valid ordering, output the tasks in order (space-separated). Otherwise, output IMPOSSIBLE.

A valid ordering is one in which for every dependency (u \rightarrow v), task (u) appears before task (v) in the ordering. In this problem, if there are multiple tasks available to be processed (i.e. tasks with no remaining dependencies), you must always choose the task with the smallest number. This ensures a unique answer when a valid ordering exists.

inputFormat

The input is given via standard input and has the following format:

  • The first line contains two integers (N) and (M), where (N) is the number of tasks and (M) is the number of dependencies.
  • The next (M) lines each contain two integers (u) and (v) (separated by a space) representing a dependency: task (u) must be done before task (v).

outputFormat

Output a single line via standard output:

  • If a valid task order exists, print the task numbers in order, separated by a single space.
  • Otherwise, print IMPOSSIBLE.## sample
4 3
0 1
1 2
2 3
0 1 2 3