dsa · medium
Detect Cycle in Undirected Graph
GreyOrangeGraph
n nodes 0..n-1, undirected edges. Return whether a cycle exists.
Arguments
n— number of nodes, labeled0..n-1edges— the edge list
**Example**
`` Input: 3 [[0,1],[1,2],[2,0]] Output: True ``
Constraints
1 <= n <= 200 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: 3 [[0,1],[1,2],[2,0]] Expected: true
Example 2
Input: 4 [[0,1],[1,2]] Expected: false