Loading repovive.com/problems/classics/18
Given a directed acyclic graph (DAG) with n vertices and m edges, find a topological ordering of the vertices using DFS-based algorithm.
A topological ordering is a linear ordering of vertices such that for every directed edge u→v, vertex u comes before v.
Output the topological ordering produced by starting DFS from the highest-numbered unvisited vertex and processing neighbors in reverse order of their vertex numbers. 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 3 2 4.
1 3 2 4
3 3 1 2 2 3 3 1
For this input, the required output is -1.
-1
6 6 1 2 1 3 2 4 3 4 4 5 4 6
For this input, the required output is 1 3 2 4 6 5.
1 3 2 4 6 5