#K2256. Connecting Water Stations
Connecting Water Stations
Connecting Water Stations
You are given a network of N water stations labeled from 1 to N, and M existing pipes connecting some pairs of these stations. Each pipe connects two stations bidirectionally.
Your task is to determine the minimum number of new pipes required to connect all the water stations, so that there is a path between any two stations.
Hint: If the network is divided into k connected components, then the minimum number of new pipes required is given by the formula: \(\text{new pipes} = k - 1\).
inputFormat
The input is read from standard input (stdin) and has the following format:
The first line contains two integers N and M, where N is the number of water stations and M is the number of existing pipes.
Each of the following M lines contains two integers u and v, indicating that there is a pipe between stations u and v.
outputFormat
Output a single integer on standard output (stdout) — the minimum number of new pipes required to ensure the entire network is connected.## sample
6 3
1 2
2 3
4 5
2