#C14545. Task Completion Ordering
Task Completion Ordering
Task Completion Ordering
In many project management and scheduling scenarios, tasks come with prerequisites that must be respected. You are given a set of n tasks and m directed dependencies between these tasks. A dependency (u, v) means that task \(u\) must be completed before task \(v\) can start.
Your goal is to determine whether it is possible to complete all the tasks while satisfying all dependencies. If it is possible, output True and a valid ordering of tasks. Otherwise, output False.
Note: The ordering should satisfy that for every directed edge \(u \to v\), task \(u\) appears before task \(v\) in the ordering.
inputFormat
The first line contains two integers \(n\) and \(m\), representing the number of tasks and the number of dependencies respectively.
Each of the next \(m\) lines contains two integers \(u\) and \(v\) indicating that task \(u\) must be completed before task \(v\) (i.e. a dependency edge from \(u\) to \(v\)).
You may assume that \(1 \leq u, v \leq n\) and \(n \ge 1\).
outputFormat
If a valid ordering exists, print True on the first line. On the second line, print a valid ordering of the tasks as \(n\) space-separated integers.
If no valid ordering exists (i.e. there is a cycle in the dependency graph), print False.
## sample4 3
1 2
2 3
3 4
True
1 2 3 4
</p>