#K86232. Task Ordering with Dependencies
Task Ordering with Dependencies
Task Ordering with Dependencies
You are given a set of tasks numbered from 1 to (n) and a set of dependencies between them. Each dependency is represented as a directed edge ((a, b)) indicating that task (a) must be completed before task (b). The problem is to determine whether there exists a valid order in which all tasks can be completed. A valid order exists if and only if the task dependency graph is a Directed Acyclic Graph ((G) is a DAG). In case of multiple valid orders, output any one of them.
Input Format: The first line contains two integers (n) (the number of tasks) and (m) (the number of dependencies). Each of the next (m) lines contains two integers (a) and (b) indicating that task (a) must be performed before task (b).
Output Format:
If a valid ordering exists, print the tasks in one line in a valid order separated by spaces; otherwise, print No valid order
.
inputFormat
The input is read from stdin. It begins with two integers (n) and (m), where (n) is the number of tasks and (m) is the number of dependencies. This is followed by (m) lines, each containing two integers (a) and (b) representing a dependency ((a) must be completed before (b)).
outputFormat
The output is written to stdout. If there exists a valid task order, output the tasks in one line separated by spaces. If no valid order exists, output No valid order
.## sample
4 3
1 2
1 3
3 4
1 2 3 4