#K73097. Task Order Scheduling
Task Order Scheduling
Task Order Scheduling
You are given n tasks (numbered from 0 to n-1) and m dependency relations. Each dependency is in the form a b, meaning that task a must be completed before task b.
Your task is to determine a valid order in which all tasks can be completed. If there exists a valid ordering, output the tasks in one valid order separated by spaces. Otherwise, if it is impossible due to a circular dependency, output impossible
.
The problem can be formalized using the concept of topological sorting in a directed graph. In particular, if we denote the dependency from task a to task b as a directed edge, then a valid ordering exists if and only if the graph is a Directed Acyclic Graph (DAG). In mathematical notation, if we let \(G=(V,E)\) be the graph with \(V=\{0, 1, \dots, n-1\}\) and \(E\) the set of dependencies, then a valid ordering is a permutation \(\pi\) of \(V\) such that for every edge \((a, b) \in E\), \(\pi(a)
inputFormat
The first line contains an integer n
(number of tasks).
The second line contains an integer m
(number of dependencies).
The following m
lines each contain two integers a
and b
, representing a dependency that task a
must be completed before task b
.
outputFormat
If a valid ordering exists, output a single line containing the ordered task numbers separated by spaces. If no valid ordering exists due to a cycle, output impossible
.
4
3
0 1
1 2
2 3
0 1 2 3