#K93992. Minimum New Roads to Connect the City

    ID: 38542 Type: Default 1000ms 256MiB

Minimum New Roads to Connect the City

Minimum New Roads to Connect the City

You are given a city with N landmarks and M existing roads connecting some pairs of these landmarks. Your task is to determine the minimum number of new roads required to ensure that every landmark in the city is connected, i.e. there is a path between any two landmarks.

The problem can be modeled as finding the number of connected components in an undirected graph. If the number of connected components is \(c\), then \(c - 1\) new roads are needed to connect the city.

Note: The landmarks are indexed from 0 to \(N-1\).

inputFormat

The input is read from stdin and consists of:

  1. A line containing two integers \(N\) and \(M\), where \(N\) is the number of landmarks and \(M\) is the number of existing roads.
  2. \(M\) subsequent lines each containing two integers \(u\) and \(v\) (0-indexed) indicating a road between landmarks \(u\) and \(v\).

outputFormat

Output a single integer to stdout representing the minimum number of new roads required to make the city fully connected.

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