#C3280. 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 graph is described by \( n \) as the number of nodes labeled from 1 to \( n \), and \( m \) edges, where each edge connects two nodes. Your task is to find the number of connected components in the graph.
A connected component is a set of nodes where there is a path between any two nodes in the set. In other words, if we denote the set of nodes in a connected component as \( C \), then for any two nodes \( u, v \in C \), there exists a sequence of edges connecting \( u \) and \( v \). The output should be a single integer representing the number of connected components.
Note: The input graph may contain self-loops and multiple edges. However, self-loops do not affect connectivity between different nodes.
inputFormat
The input is read from standard input (stdin) and has the following format:
n m u1 v1 u2 v2 ... um vm
Here, the first line contains two integers \( n \) and \( m \) separated by a space. \( n \) is the number of nodes and \( m \) is the number of edges. Each of the next \( m \) lines contains two integers \( u_i \) and \( v_i \) representing an undirected edge connecting node \( u_i \) and node \( v_i \).
outputFormat
Output a single integer to standard output (stdout) that denotes the number of connected components in the graph.
## sample5 3
1 2
2 3
4 5
2