#C8553. Cycle Detection in an Undirected Graph
Cycle Detection in an Undirected Graph
Cycle Detection in an Undirected Graph
You are given an undirected graph with \(N\) nodes and \(M\) edges. The graph is described with a list of edges. Your task is to determine whether the graph contains a cycle.
If a cycle exists in any of the connected components of the graph, print 1
. Otherwise, print 0
.
Note: The nodes are labeled from 1 to \(N\). The input is read from standard input (stdin) and the output should be written to standard output (stdout).
inputFormat
The first line contains two integers, \(N\) and \(M\), which represent the number of nodes and edges in the graph respectively. The following \(M\) lines each contain two integers \(u\) and \(v\) indicating an undirected edge between nodes \(u\) and \(v\>.
outputFormat
Output a single line containing a single integer: 1
if the graph contains a cycle, and 0
otherwise.
3 3
1 2
2 3
3 1
1
</p>