Redundant Connection
A tree is an undirected, connected graph with n nodes and exactly n - 1 edges and no cycles. You are given a graph on n nodes, labelled 1 to n, that started as a tree and had **one extra edge added**, given as edges, a list of n [u, v] pairs (so the graph has n edges and exactly one cycle).
Return the edge that can be removed so that the remaining n - 1 edges form a tree again. If more than one edge could be removed, return the one that occurs **last** in edges.
Example cases
- trianglein edges =121323out [2,3]1-2 and 1-3 form a tree; 2-3 closes the cycle and is the last edge that does so.
- cycle plus a branchin edges =1223341415out [1,4]1-2-3-4 plus 1-4 closes a cycle; 1-5 is a harmless branch off the tree.
- cycle formed earlyin edges =1223133445out [1,3]1-2 and 2-3 form a tree; 1-3 closes the cycle. The remaining edges just extend the branch.
Constraints
- n == edges.length
- 3 <= n <= 1000
- edges[i].length == 2
- 1 <= u, v <= n
- u != v
- There are no repeated edges.
- The given graph is connected and contains exactly one cycle.
edges =
[[1,2],[1,3],[2,3]]