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
4mo

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!
Select
Add answer anonymously...

Atlassian Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Design an API service to limit the number of accesses for a user to a function w...read more
A Software Developer Intern was asked Q. Longest increasing subsequence
A Software Developer Intern was asked Q. Ninja and Geometry Problem Statement In this problem, Ninja is provided with two...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Design an API service to limit the number of accesses for a user to a function w...read more
A Software Developer Intern was asked 12mo agoQ2. Longest increasing subsequence
A Software Developer Intern was asked Q3. Ninja and Geometry Problem Statement In this problem, Ninja is provided with two...read more
Atlassian Software Developer Intern 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