Doctor Ninja's House Problem Statement

In a network of 'N' cities with 'M' paths connecting them, Doctor Ninja aims to purchase a house in a city 'X' such that it is possible to reach every other city from city 'X'. Your task is to determine the city 'X'.

If there are multiple options for city 'X', select the city with the smallest number. If no such city exists, return -1.

Example:

Input:
T = 1
N = 8, M = 10
Paths:
0 1
0 2
1 3
2 3
2 4
3 5
4 5
4 6
5 7
6 7
Output:
0
Explanation:

In the given graph configuration, city 0 can reach all the other cities either directly or indirectly, making it the mother vertex. Thus, the output is 0.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 104
  • 1 <= M <= 105
  • 0 <= X < N
  • Time Limit: 1 sec.
Note:

You are not responsible for printing anything, as this is handled externally. Your task is to implement the function to find the solution.

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

Help your peers!
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

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