#K74677. Minimum Additional Bus Routes

    ID: 34251 Type: Default 1000ms 256MiB

Minimum Additional Bus Routes

Minimum Additional Bus Routes

You are given a network of bus stops labeled from 0 to n-1 and a list of bidirectional routes between some of these stops. Your task is to determine the minimum number of additional routes required to connect the entire network such that there is a path (direct or indirect) between any two bus stops.

In graph theory terms, the bus stops represent nodes and the routes represent edges. The network may consist of several connected components. In order to connect c disconnected components, at least c - 1 additional routes are required. If there is only one bus stop, no additional route is needed.

Input is provided via standard input and output should be printed to standard output.

inputFormat

The first line contains two integers n and m, where n is the number of bus stops and m is the number of existing routes. This is followed by m lines, each containing two integers u and v which represent an existing bidirectional route between stops u and v.

outputFormat

Output a single integer representing the minimum number of additional routes required to connect all bus stops.## sample

5 3
0 1
1 2
3 4
1