#K80492. Connecting Rooms

    ID: 35542 Type: Default 1000ms 256MiB

Connecting Rooms

Connecting Rooms

You are given a building with N rooms (numbered from 0 to N-1) and M corridors connecting some pairs of rooms. Your task is to determine the minimal number of additional corridors required to connect all the rooms so that it is possible to travel from any room to any other room.

The answer is equal to the number of connected components minus one. In other words, if the building is divided into k connected parts, you must add k-1 corridors to connect them all.

Input and Output Format: Read input from standard input and write your output to standard output.

Note: If the building is already fully connected, then no additional corridors are needed.

For example, if N = 4 and corridors connect rooms 0-1 and 2-3, then there are 2 connected groups and you need 1 additional corridor.

inputFormat

The first line contains two integers N and M separated by a space. Each of the next M lines contains two integers u and v, representing a corridor between room u and room v.

outputFormat

Output a single integer on a line: the minimal number of additional corridors required to connect all rooms.## sample

4 2
0 1
2 3
1