Doctor Ninja's House

There are ‘N’ cities and ‘M’ paths connecting them. Doctor Ninja wants to purchase a house in a city ‘X’ such that she can reach any city from the city ‘X’.

Your task is to help Ninja to find her a house ‘X’, if you are given with ‘N’ cities and ‘M’ paths.

There can be more than one house ‘X’ possible. You need to output the house ‘X’ with minimum value. If you are unable to find any such house ‘X’, then return -1.

A mother vertex in a graph G = (V, E) is a vertex v such that all other vertices in G can be reached by a path from v.

Example:
Consider the following Graph: 

Vertices reachable from vertex 0:
0 -> 1 -> 3 -> 2 -> 4 -> 5 -> 7 -> 6

Vertices reachable from vertex 1:
1 -> 3 -> 2 -> 4 -> 5 -> 7 -> 6

Vertices reachable from vertex 2:
2 -> 1 -> 3 -> 4 -> 5 -> 7 -> 6

Vertices reachable from vertex 3:
3 -> 2 -> 1 -> 4 -> 5 -> 7 -> 6

Vertices reachable from vertex 4:
4 -> 5 -> 7 -> 6

Vertices reachable from vertex 5:
5 -> 7 -> 6 -> 4

Vertices reachable from vertex 6:
6 -> 4 -> 5 -> 7

Vertices reachable from vertex 7:
7 -> 6 -> 4 -> 5

Clearly, there is only one vertex “ 0 ” in the graph, from which all other vertices are reachable. Hence, vertex “ 0 ” is the mother vertex of the above graph.

There can be more than one mother vertices in a graph.

For Example, in the above graph, vertices 0, 1 and 2 are mother vertices.

Input Format:
The first line contains an Integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains two space separated integers ‘N’, denoting the number of cities (from 0 to ‘N-1’) and ‘M’, denoting the number of paths connecting cities.

Next ‘M’ lines contain two space separated integers ‘u’ and ‘v’ denoting a path from a city ‘u’ to a city ‘v’, where { u, v } ∈ N.
Output Format:
For each test case, return an integer ‘X’, where ‘X’ is the minimum city which Doctor can buy. 
There can be more than one house ‘X’ possible. You need to output the house ‘X’ with minimum value. If you are unable to find any such house ‘X’, then return -1.
The output of each test case should be printed in a separate line.
Note:
You are not required to print anything, it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 10
1 <= N <= 10^4
1 <= M <= 10^5
0 <= X < N

Time Limit : 1 sec.
AnswerBot
1y

The task is to find a city 'X' from which all other cities can be reached in a given graph.

  • Find the mother vertices in the graph.

  • If there are multiple mother vertices, return the minimum value.

  • If ther...read more

CodingNinjas
author
2y
Naive Approach

A trivial approach will be to perform a DFS on all the vertices and find whether we can reach all the vertices of the graph from that vertex or not.

Algorithm :


1. Implement the function ...read more

CodingNinjas
author
2y
Optimized Approach

The idea is based on Kosaraju’s Strongly Connected Component Algorithm. In a graph of strongly connected components, mother vertices are always vertices of the source component in th...read more

Add answer anonymously...
Wunderman Thompson Commerce Technical Associate Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter