#K55472. Connected Components in an Undirected Graph
Connected Components in an Undirected Graph
Connected Components in an Undirected Graph
You are given an undirected graph with \(N\) nodes and \(M\) edges. The nodes are numbered from 1 to \(N\). Your task is to determine the number of connected components in the graph.
An undirected graph is said to be connected if there is a path between every pair of vertices. Otherwise, the graph consists of several connected components. In this problem, a connected component is a maximal set of nodes that are connected directly or indirectly through the given edges.
Input Format: The first line contains two integers \(N\) and \(M\). Each of the following \(M\) lines contains two integers \(U\) and \(V\) representing an undirected edge between nodes \(U\) and \(V\).
Output Format: Output a single integer representing the number of connected components in the graph.
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains two space-separated integers \(N\) and \(M\).
- Each of the next \(M\) lines contains two space-separated integers \(U\) and \(V\) indicating an edge in the graph.
outputFormat
Output the number of connected components in the graph. The output should be printed to standard output (stdout).
## sample5 3
1 2
2 3
4 5
2