Reorder Edges Problem Statement

Given a connected directed acyclic graph with 'N' nodes and 'N-1' edges, where each pair of nodes is connected by exactly one edge, you can perform the following operation any number of times:

  1. Select two nodes 'X' and 'Y' having a direct edge between them.
  2. Reverse the direction of the edge; if it is from 'X' to 'Y', change it to 'Y' to 'X', and vice versa.

Your objective is to reorder the edges such that there is a directed path from each node to node 0 with the minimum number of edge reversals.

Input:

The first line contains an integer 'T', the number of test cases.
Each test case starts with an integer 'N', the number of nodes in the graph.
Then follow 'N-1' lines, each containing two integers 'U' and 'V' indicating a direct edge from 'U' to 'V'.

Output:

For each test case, output the minimum number of operations needed to ensure a directed path exists from every node to node 0.

Example:

Input:
T = 1
N = 5
Edges: (0,1), (1,2), (3,2), (4,3)
Output:
2

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 3000
  • 0 <= U, V <= N - 1
  • Time Limit: 1 second

Note:

You do not need to print anything as the task is to implement the function which performs the operation.

AnswerBot
6d

The task is to implement a function that reorders edges in a directed acyclic graph to ensure a directed path exists from every node to node 0 with minimum edge reversals.

  • Iterate through the graph to ...read more

Help your peers!
Add answer anonymously...
Atlassian Software Developer Intern 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