A rooted tree here is a directed graph with one root that has no parent, where every other node has exactly one parent and can be reached from the root by following edges.
You get such a tree on nodes 1 to n, plus one extra directed edge [u, v] (u ≠ v, and not a copy of an existing edge). The n edges come as a list edges, each [parent, child].
Return an edge whose removal leaves a rooted tree on all n nodes. If several edges work, return the one that comes last in edges.
Example 1
Input: edges = [[2, 1], [3, 1], [4, 2], [1, 4]] Output: [2, 1] Why: 1 has two parents; dropping [3, 1] leaves the loop 1 → 4 → 2 → 1
2 more examples and the constraints
Example 2
Input: edges = [[3, 1], [1, 2], [2, 3], [1, 4]] Output: [2, 3] Why: no node has two parents; 3 → 1 → 2 → 3 is a loop, [2, 3] is its last edge
Example 3
Input: edges = [[1, 2], [2, 4], [1, 3], [3, 4]] Output: [3, 4] Why: 4 has two parents and there is no loop, so drop the later one
Constraints
- 3 ≤ n ≤ 1,000, and edges has exactly n entries
- 1 ≤ u, v ≤ n and u ≠ v
- The input is always a rooted tree plus one added edge
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Remove each edge and checkO(n²)
- Two parents, then union-findO(n·α(n))
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for n = 1,000 | Verdict |
|---|---|---|---|
| Remove each edge and check | O(n²) | Simple; rechecks the whole graph each time | |
| Two parents, then union-find | O(n·α(n)) | Best; two passes |
n = number of nodes, which equals the number of edges. α is the inverse Ackermann function, at most 4 in practice.
Edge cases to test
- Loop back to the root. No node has two parents (Example 2); return the last edge of the loop.
- Two parents, no loop. Example 3: return the later of the two parent edges.
- Two parents and a loop. Example 1: only the parent edge that lies on the loop can go, even if it comes first.
- Extra edge from the root. With [[1, 2], [2, 3], [1, 3]], node 3 has two parents and there is no loop, so the later one, [1, 3], goes.
- Large chain. 1 → 2 → … → 1,000 plus [1000, 1] is one big loop through the root.
Common mistakes
- Solving it like the undirected version: returning the edge that closes a loop even when it is not one of the two parent edges.
- Skipping first instead of second in pass 2, which reports the wrong edge when both would work.
- Forgetting the case with no two-parent node, where the extra edge points at the root.
How to talk through it
- 1Clarify. Edges are [parent, child]? Exactly one extra edge? If several answers work, the last one in the list?
- 2Split the cases. The extra edge either gives a node two parents, makes a loop, or both.
- 3Give the brute force. Drop each edge from the end and check for a valid rooted tree: O(n²).
- 4Use union-find. Find the two parent edges, union everything except the later one, and let a loop decide between them.
- 5Test out loud. Run the three cases: loop only, two parents only, and both, as in the examples.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?