#K196. Connected Components in an Undirected Graph

    ID: 24630 Type: Default 1000ms 256MiB

Connected Components in an Undirected Graph

Connected Components in an Undirected Graph

Given an undirected graph with NN nodes and MM edges, determine the number of connected components in the graph. A connected component is a set of nodes where every node is reachable from any other node within the same set. Note that isolated nodes (nodes with no edges) are considered as separate connected components.

Input Graph Representation:
- The first line contains two integers NN and MM, representing the number of nodes and edges respectively.
- The following MM lines each contain two integers uu and vv, denoting an undirected edge between node uu and node vv.

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

inputFormat

The input is provided via standard input (stdin). The first line contains two space-separated integers NN and MM, where NN is the number of nodes and MM is the number of edges. Each of the next MM lines contains two space-separated integers uu and vv, representing an edge between nodes uu and vv.

outputFormat

Output a single integer to standard output (stdout) representing the number of connected components in the graph.## sample

5 3
1 2
1 3
4 5
2

</p>