Path Reversals Problem Statement

You are provided with a directed graph and two nodes, 'S' and 'D'. Your objective is to determine the minimum number of edges that need to be reversed to establish a path from node 'S' to node 'D'.

Input:

The first line of input contains an integer 'T' representing the number of test cases.

The first line of each test case contains two space-separated integers, 'N' and 'M', representing the number of nodes and edges in the graph.

The following M lines of the test case contain two space-separated integers, 'A' and 'B', representing an edge from node 'A' to node 'B'.

The last line of the test case contains two space-separated integers, 'S' and 'D', denoting the start and end node.

Output:

For each test case, output a single integer, which is the minimum number of edges that need to be reversed to create a path from 'S' to 'D'.

Each test case's result should be printed on a new line.

Example:

Consider 'S' = 0 and 'D' = 4. In the given graph, a path from node 0 to node 4 can be achieved by reversing only the edge (0, 5). Therefore, the answer is 1.

Constraints:

  • 1 <= T <= 5
  • 2 <= N <= 105
  • N - 1 <= M <= 105
  • 0 <= A, B < N
  • 0 <= S, D < N
  • Time Limit: 1 sec

Note:

There is no need to print anything, as it is already handled. Simply implement the function and return the result.
AnswerBot
1y

The task is to find the minimum number of edges that need to be reversed in a directed graph to find the path from a start node to an end node.

  • The problem can be solved using graph traversal algorithm...read more

Help your peers!
Select
Add answer anonymously...

Myntra Mern Stack Developer interview questions & answers

A Mern Stack Developer was asked Q. Trapping Rain Water Problem Statement You are given a long type array/list ARR o...read more
A Mern Stack Developer was asked Q. Path Reversals Problem Statement You are provided with a directed graph and two ...read more
A Mern Stack Developer was asked Q. Triplets with Given Sum Problem Given an array or list ARR consisting of N integ...read more
Myntra Mern Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits