#K35557. Taco Topological Sort
Taco Topological Sort
Taco Topological Sort
You are given a directed acyclic graph (DAG) representing task dependencies. Your job is to determine an order in which all tasks can be performed such that for every directed edge \( u \rightarrow v \), task \( u \) comes before task \( v \) in the ordering. If no such ordering exists (i.e. if the graph contains a cycle), output Cycle detected
.
The input begins with two integers \( N \) and \( M \) where \( N \) is the number of tasks (labeled from 1 to \( N \)) and \( M \) is the number of dependency relations. Each of the following \( M \) lines contains two integers \( u \) and \( v \) representing a dependency (an edge from \( u \) to \( v \)).
Note: If there are multiple valid topological orderings, your program may output any one of them.
inputFormat
The first line of input contains two integers \( N \) and \( M \) (1 \( \leq N \leq 10^5 \), 0 \( \leq M \leq 10^5 \)). The next \( M \) lines each contain two integers \( u \) and \( v \), indicating a directed edge from task \( u \) to task \( v \).
Input is provided via standard input.
outputFormat
If the tasks can be topologically sorted, output the ordering as a sequence of \( N \) space-separated integers. If there is a cycle in the dependency graph, output Cycle detected
(without quotes).
Output should be written to standard output.
## sample5 4
1 2
2 3
3 4
4 5
1 2 3 4 5