Connectivity components
Problem
Count the number of connected components in an undirected graph. There can be loops and multiple edges in a graph.
Input: First, the first line contains two numbers N and M, setting respectively the number of vertices and the number of edges (1<=N<= 100, 0<=M<=10000), and then the edges are listed. Each edge is defined by the two vertex numbers it connects
Output: Print a single number - the number of connected components
Examples
# |
Input |
Output |
1 |
3 4
1 1
1 2
1 3
2 3
|
1 |
2 |
5 3
1 1
1 2
2 1
|
4 |
3 |
5 0 |
5 |