dsa · medium

Detect Cycle in Undirected Graph

n nodes 0..n-1, undirected edges. Return whether a cycle exists.

Arguments

Example

`` Input: 3 [[0,1],[1,2],[2,0]] Output: true ``

Constraints

1 <= n <= 200 Undirected simple graph: u != v; [u, v] and [v, u] are the same edge. 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

Open in the Dojo editor