#K36412. Minimum Additional Roads

    ID: 25749 Type: Default 1000ms 256MiB

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 and M, where N is the number of houses and M is the number of roads.
  • The next M lines each contain two space-separated integers u and v representing a road connecting houses u and v.

outputFormat

Output a single integer to stdout: the minimum number of additional roads required to make the entire network connected.

## sample
6 5
1 2
2 3
3 4
4 5
5 6
0