#K196. Connected Components in an Undirected Graph
Connected Components in an Undirected Graph
Connected Components in an Undirected Graph
Given an undirected graph with nodes and 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 and , representing the number of nodes and edges respectively.
- The following lines each contain two integers and , denoting an undirected edge between node and node .
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 and , where is the number of nodes and is the number of edges. Each of the next lines contains two space-separated integers and , representing an edge between nodes and .
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>