Detect Cycle in Undirected Graph
Mediumjavapythonccppjavascript
Given an undirected graph, determine whether it contains at least one cycle.
Input Format
The first line contains two integers V and E. Each of the next E lines contains an undirected edge u v.
Output Format
Print 1 if the graph contains a cycle, otherwise print 0.
Example 1
Input
3 3 0 1 1 2 2 0
Output
1
Explanation: The three vertices form a closed loop.
- 1 <= V <= 100000
- 0 <= E <= 200000
- 0 <= u, v < V
- There are no self-loops or repeated edges.