#K68687. Minimum Additional Roads to Connect the City

    ID: 32920 Type: Default 1000ms 256MiB

Minimum Additional Roads to Connect the City

Minimum Additional Roads to Connect the City

You are given the description of a city's road network with n intersections and m bidirectional roads connecting some of these intersections. Your task is to determine the minimum number of additional roads required to make the entire road network fully connected. In other words, there should be a path between any pair of intersections.

Note that if the number of connected components in the network is k, then you need at least \(k-1\) additional roads to connect all components.

Input/Output: Read input from stdin and print the result to stdout.

inputFormat

The first line contains two integers n and m where:

  • n (1 ≤ n) is the number of intersections.
  • m (0 ≤ m) is the number of roads.

The next m lines each contain two integers u and v representing a bidirectional road connecting intersections u and v.

outputFormat

Print a single integer representing the minimum number of additional roads required to make the road network fully connected.

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