#C6263. Connected Components in an Undirected Graph

    ID: 50004 Type: Default 1000ms 256MiB

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 represented by its nodes numbered from 1 to \(n\) and a list of \(m\) edges. Your task is to determine the number of connected components in the graph.

A connected component is a set of nodes where every pair of nodes is connected by a path. If a node is isolated and has no connecting edge, it is considered as a connected component by itself.

Input Format: The first line contains two integers \(n\) and \(m\) separated by a space, denoting the number of nodes and the number of edges respectively. The following \(m\) lines each contain two integers representing an edge between two nodes.

Output Format: Print a single integer, the number of connected components in the graph.

inputFormat

The first line contains two integers \(n\) and \(m\) \( (1 \leq n \leq 10^5, 0 \leq m \leq 10^5) \), representing the number of nodes and edges respectively. The next \(m\) lines each contain two integers \(u\) and \(v\) representing an undirected edge between node \(u\) and node \(v\).

outputFormat

Output a single integer indicating the number of connected components in the graph.

## sample
6 4
1 2
2 3
3 4
5 6
2

</p>