#C448. Task Scheduling
Task Scheduling
Task Scheduling
You are given n tasks and m dependencies between them. Each dependency is given as a directed edge u → v indicating that task u must be completed before task v. Your task is to determine an ordering of the tasks that satisfies all the dependencies. If no such ordering exists (i.e. there is a cycle in the dependency graph), output IMPOSSIBLE
.
The dependency condition can be mathematically expressed using LaTeX as follows:
\(\forall\ (u,v) \in D,\; pos(u) < pos(v)\)
If multiple valid orderings exist, output any one of them. The input is provided via standard input (stdin) and the result should be printed to standard output (stdout).
inputFormat
The first line of input contains two integers n and m representing the number of tasks and the number of dependencies respectively. The following m lines each contain two integers u and v, representing a dependency where task u must precede task v.
Example:
5 4 1 2 2 3 3 4 5 3
outputFormat
If a valid ordering exists, output the tasks in one valid order separated by spaces. If no valid ordering exists, output the string IMPOSSIBLE
.
Example valid output:
1 5 2 3 4## sample
5 4
1 2
2 3
3 4
5 3
1 5 2 3 4