#K96002. Cycle Detection in Undirected Graph
Cycle Detection in Undirected Graph
Cycle Detection in Undirected Graph
You are given an undirected graph with \(n\) nodes and \(m\) edges. The graph is defined by \(m\) pairs of nodes, each representing an edge. Your task is to determine whether the graph contains a cycle.
A cycle in an undirected graph is a path that starts and ends at the same vertex with at least one edge, and no edge is repeated. Note that the graph might be disconnected. You need to check all connected components for the existence of a cycle.
Input: The first line contains two integers \(n\) and \(m\). Each of the next \(m\) lines contains two integers representing an undirected edge between two nodes.
Output: Print "YES" if there is at least one cycle in the graph, otherwise print "NO".
inputFormat
The input is read from standard input (stdin) and has the following format:
n m u1 v1 ... um vm
where \(n\) is the number of nodes, \(m\) is the number of edges, and each \(u_i, v_i\) denotes an edge between nodes \(u_i\) and \(v_i\).
outputFormat
Output a single line to standard output (stdout) containing "YES" if the graph has a cycle or "NO" if it does not.
## sample5 0
NO