#C4436. Group Formation from Handshakes
Group Formation from Handshakes
Group Formation from Handshakes
You are given N people numbered from 1 to N, and M handshakes represented by pairs of integers. Each handshake connects two people. Two people are considered to be in the same group if they are directly or indirectly connected by handshakes. Your task is to determine the number of distinct groups formed.
Detailed Explanation:
- If there are no handshakes, then each person forms their own group.
- If one handshake connects two people, they are in one group.
- If a person shakes hands with someone else who is already connected to another person (or group), all of them form a single group.
The mathematical formulation can be expressed using connectivity in an undirected graph. In particular, you need to calculate the number of connected components in the graph:
$$\text{Number of Groups} = \text{Number of Connected Components in the Graph}$$
inputFormat
The first line of input contains two integers N and M, where N is the number of people and M is the number of handshakes. The following M lines each contain two space-separated integers representing a handshake between two people.
outputFormat
Output a single integer representing the number of groups formed from the handshakes.
## sample5 3
1 2
2 3
4 5
2