#K73452. Connected Components Count
Connected Components Count
Connected Components Count
You are given an undirected graph with \(N\) nodes and \(M\) edges. The graph is represented by \(M\) pairs of vertices, where each pair \((u, v)\) indicates an undirected edge connecting node \(u\) and node \(v\).
Your task is to determine the number of connected components in the graph. A connected component is a subset of nodes such that any two nodes in the subset are connected by paths, and no node in the subset is connected to a node outside of it.
The input is provided via standard input and the output should be printed to standard output. Read the details in the input and output descriptions.
inputFormat
The first line contains two integers (N) and (M) representing the number of nodes and edges respectively. The next (M) lines each contain two integers (u) and (v), representing an undirected edge between nodes (u) and (v). It is guaranteed that the nodes are labeled from 1 to (N).
outputFormat
Print a single integer, the number of connected components in the graph.## sample
4 2
1 2
2 3
2