#K36412. Minimum Additional Roads
Minimum Additional Roads
Minimum Additional Roads
You are given a network of N houses numbered from 1 to N and M bidirectional roads connecting some pairs of houses. The goal is to ensure that every house is reachable from every other house.
Your task is to compute the minimum number of additional roads required to connect all the houses. If there are k connected components in the network, then the answer is given by the formula:
[ \text{Answer} = k - 1 ]
Read the input from stdin
and output the result to stdout
.
inputFormat
The input is read from stdin
and has the following format:
- The first line contains two space-separated integers
N
andM
, whereN
is the number of houses andM
is the number of roads. - The next
M
lines each contain two space-separated integersu
andv
representing a road connecting housesu
andv
.
outputFormat
Output a single integer to stdout
: the minimum number of additional roads required to make the entire network connected.
6 5
1 2
2 3
3 4
4 5
5 6
0