Algorithms Playbook
← All problems
MediumUnion-find →Similar to: Redundant Connection

The cable that makes a loop

Problem

A network of n nodes (numbered 1..n) was a tree, then one extra edge was added. Given the list of n edges in the order they were added, return the edge that can be removed to leave a tree. If several answers exist, return the one that appears last.

Examples

Input: [[1,2],[1,3],[2,3]]

Output: [2,3]

Input: [[1,2],[2,3],[3,4],[1,4],[1,5]]

Output: [1,4]

Hints

    Try it first. Every hint reveals a bit more.

    Solution