Skip to content

Redundant Connection

MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.

A tree with n nodes labelled 1 to n had one extra edge added, so edges now has n undirected edges and exactly one cycle. Return an edge you can remove to turn the graph back into a tree. If several edges would work, return the one that appears last in edges.

edges = [[1, 2], [1, 3], [2, 3]]                  →  [2, 3]
edges = [[1, 2], [2, 3], [3, 4], [1, 4], [1, 5]]  →  [1, 4]

Constraints

  • 3 <= n <= 1000
  • edges.length == n
  • 1 <= a < b <= n for every edge [a, b], and there are no repeated edges
  • The graph is connected