SDE Path

Detect Cycle in an Undirected Graph (BFS)

Medium

Detect Cycle in an Undirected Graph (BFS)

Determine whether an undirected graph contains a cycle using BFS.

Input format

The first line contains N and M. The next M lines contain edge pairs (undirected).

Output format

Print true if the graph contains a cycle (using BFS), false otherwise.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
5 10
3 4
0 2
0 4
1 4
1 3
0 1
1 2
0 3
2 4
2 3
Expected output
true
Example 2
Input
5 7
2 3
0 1
2 4
1 3
1 2
0 4
0 2
Expected output
true