#K65322. Connecting the Town
Connecting the Town
Connecting the Town
You are given a town with n houses and m bidirectional roads connecting some of them. Each road connects two houses. Your task is to determine the minimum number of additional roads required such that there is a path between any two houses in the town.
Formally, if the town has several connected components, you must add roads to connect them into a single connected component. The answer is equal to the number of connected components minus one.
Note: Houses are numbered from 1 to n.
inputFormat
The first line of input contains two integers n and m (1 ≤ n ≤ 10^5, 0 ≤ m ≤ 10^5), representing the number of houses and the number of roads respectively. The following m lines each contain two integers u and v (1 ≤ u, v ≤ n, u ≠ v), indicating that there is a road between house u and house v.
outputFormat
Output a single integer, which is the minimum number of new roads required to make the entire town connected.## sample
5 3
1 2
1 3
4 5
1