1016. Find Mother Vertex in Directed Graph

EasyGraphsBFSDFSGraph

Given a directed graph with n nodes and a list of directed edges, a mother vertex is a node from which all other nodes are reachable. Return the smallest-labeled mother vertex, or -1 if none exists. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Integer — the smallest mother vertex, or -1.

Examples

Example 1
Input: {"n":4,"edges":[[0,1],[0,2],[1,3],[2,3]]}
Output: 0
Explanation: All nodes reachable from 0.
Example 2
Input: {"n":3,"edges":[[0,1],[2,1]]}
Output: -1
Explanation: No single source reaches all.
Example 3
Input: {"n":3,"edges":[[0,1],[1,2]]}
Output: 0
Explanation: 0 reaches 1 and 2.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →