#K82217. Task Dependency Ordering
Task Dependency Ordering
Task Dependency Ordering
You are given T tasks and D dependency relations between them. Each dependency is given as a pair of integers \(a\) and \(b\) indicating that task \(a\) must be completed before task \(b\). Your task is to determine a valid sequence to complete all tasks while respecting all the dependencies. If no such ordering exists (i.e. there is a cycle in the dependency graph), output IMPOSSIBLE
.
Note: The dependency relations can be modeled as a directed graph where a valid ordering is a topological sort of the graph. In particular, for tasks \(a\) and \(b\), the dependency is represented as \(a \rightarrow b\). If the graph contains a cycle, then no topological order exists.
inputFormat
The input is read from stdin and is formatted as follows:
- The first line contains two space-separated integers: \(T\) (the number of tasks) and \(D\) (the number of dependencies).
- The next \(D\) lines each contain two space-separated integers \(a\) and \(b\), indicating that task \(a\) must be completed before task \(b\).
outputFormat
Output to stdout a single line. If a valid ordering exists, print the tasks in one valid order separated by a single space. Otherwise, print IMPOSSIBLE
.
4 3
1 2
1 3
3 4
1 2 3 4