Redundant Connection II - Problem

In this problem, a rooted tree is a directed graph such that there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.

The given input is a directed graph that started as a rooted tree with n nodes (with distinct values from 1 to n), with one additional directed edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u_i, v_i] that represents a directed edge connecting nodes u_i and v_i, where u_i is a parent of child v_i.

Return an edge that can be removed so that the resulting graph is a rooted tree of n nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.

Input & Output

Example 1 — Node with Two Parents
$ Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]
💡 Note: Node 3 has two parents: 1 and 2. Removing the later edge [2,3] creates a valid rooted tree with root 1.
Example 2 — Cycle without Double Parent
$ Input: edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
Output: [4,1]
💡 Note: There's a cycle 1→2→3→4→1. No node has two parents, so we remove the last edge in the cycle.
Example 3 — Simple Case
$ Input: edges = [[1,2],[2,3],[3,1]]
Output: [3,1]
💡 Note: Forms a cycle 1→2→3→1. The last edge [3,1] creates the cycle, so remove it.

Constraints

  • 3 ≤ edges.length ≤ 1000
  • edges[i].length == 2
  • 1 ≤ ui, vi ≤ edges.length
  • ui ≠ vi

Visualization

Tap to expand
Redundant Connection II Union-Find with Case Analysis INPUT Directed Graph (Tree + 1 edge) 1 2 3 redundant? edges array: [1,2] [1,3] [2,3] idx 0 idx 1 idx 2 n = 3 nodes Node 3 has 2 parents! ALGORITHM STEPS 1 Find Two-Parent Node Node 3: parents [1,3] & [2,3] cand1=[1,3], cand2=[2,3] 2 Initialize Union-Find parent[i] = i for all nodes Skip cand2 temporarily 3 Process Edges [1,2]: union(1,2) OK [1,3]: union(1,3) OK [2,3]: skipped (cand2) 4 Check for Cycle No cycle without cand2 --> cand2 is redundant Union-Find State: parent = [_, 1, 1, 1] FINAL RESULT Valid Rooted Tree: 1 root 2 3 removed Output: [2, 3] OK - Valid tree formed! Each node has 1 parent (except root) Key Insight: Two cases: (1) A node has two parents, or (2) There's a cycle. If a node has two parents, one edge must go. Try removing the later edge first (cand2). If no cycle forms, cand2 is the answer. Otherwise, remove cand1. Union-Find efficiently detects cycles in O(n * alpha(n)) time, where alpha is the inverse Ackermann function. TutorialsPoint - Redundant Connection II | Union-Find with Case Analysis
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
98.2K Views
Medium Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen