step1. Do topological sorting of vertices of directed graph.
step2. Now check whether we can reach all vertices from first vertex of topologically sorted vertices in step 1.
To perform a step 2, again initialize
array discovered[i] to false and do dfs startin from first node of topologically sorted vertices.
If all vertices can be reached, then graph has mother vertex, and mother vertex will be the former of topologically sorted vertices.
time complexity:
step1 takes O(n + m)
, step 2 takes O(n + m)
so total O(n+m) + O(n+m) = O(n+m)