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:
- Select two nodes 'X' and 'Y' having a direct edge between them.
- 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.
Be the first one to answer
Add answer anonymously...
Top Atlassian Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Atlassian Software Developer Intern
>
Atlassian Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
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