Loading repovive.com/problems/classics/17
Given a directed acyclic graph (DAG) with n vertices and m edges, find a topological ordering of the vertices using Kahn's algorithm (BFS-based).
A topological ordering is a linear ordering of vertices such that for every directed edge u→v, vertex u comes before v.
If multiple valid orderings exist, output the lexicographically smallest one.
If the graph contains a cycle, output -1.
4 4 1 2 1 3 2 4 3 4
For this input, the required output is 1 2 3 4.
1 2 3 4
3 3 1 2 2 3 3 1
For this input, the required output is -1.
-1
5 4 5 1 5 2 1 3 2 3
For this input, the required output is 4 5 1 2 3.
4 5 1 2 3