#K36877. Task Order Determination
Task Order Determination
Task Order Determination
You are given n tasks, numbered from 1 to n, and m dependency relations between them. Each dependency is given as a pair of integers (a, b) meaning that task a
must be completed before task b
. Your goal is to determine a valid order in which all tasks can be completed while satisfying all these dependencies.
If there is a cycle in the dependencies, then it is impossible to complete all tasks and you should output 0
.
Formally, find an ordering of tasks such that for every dependency \(a \rightarrow b\), task a
appears before task b
in the ordering. If no valid ordering exists, output 0
.
inputFormat
The input is read from stdin
and has the following format:
n m a1 b1 a2 b2 ... am bm
Where:
n
is the number of tasks.m
is the number of dependency pairs.- Each of the next
m
lines contains two integersa
andb
indicating that taska
must be done before taskb
.
outputFormat
The output is printed to stdout
. If a valid task order exists, print the tasks in one valid order separated by a single space. If no valid order exists due to a cycle, print 0
.
3 0
1 2 3