Redundant Connection
Medium
LC #684
union-findcycle detectionNot attempted yet
A tree with n nodes (labelled 1..n) had one extra
edge added, so the undirected graph given as edges now
contains exactly one cycle.
Return an edge you can remove so the graph becomes a tree
again. If several edges qualify, return the one that
appears last in edges.
Example 1
Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]
Example 2
Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]
The cycle is 1-2-3-4-1; [1,4] is its last edge in the
input.
Constraints
3 <= n <= 2 * 10^4,len(edges) == n1 <= a < b <= nfor each edge[a, b], no duplicates- the graph is connected