#K52967. Project Management Tool: Dependency Order
Project Management Tool: Dependency Order
Project Management Tool: Dependency Order
You are given a set of projects and a list of dependencies between these projects. Each dependency is a directed relation \(A \to B\), meaning project \(A\) must be completed before project \(B\). Your goal is to determine a valid order in which all projects can be completed. If there exists a cycle making it impossible to complete all projects, output \(\text{IMPOSSIBLE}\).
This problem can be solved using topological sorting. If there is a valid ordering, output the projects as a space-separated sequence of \(N\) integers; otherwise, output \(\text{IMPOSSIBLE}\).
inputFormat
The input is read from stdin and has the following format:
N D A1 B1 A2 B2 ... AD BD
Here, \(N\) is the number of projects and \(D\) is the number of dependency pairs. Each of the following \(D\) lines contains two integers \(A\) and \(B\) representing a dependency that project \(A\) must be completed before project \(B\).
outputFormat
The output should be written to stdout. If a valid order of completion exists, print a space-separated sequence of \(N\) integers corresponding to the order in which the projects should be completed. If no valid order exists (i.e. there is a cycle), print IMPOSSIBLE
.
4 3
1 2
2 3
3 4
1 2 3 4
</p>