#K4626. Network Resilience Enhancement

    ID: 27937 Type: Default 1000ms 256MiB

Network Resilience Enhancement

Network Resilience Enhancement

You are given an undirected network with n nodes (numbered from 1 to n) and m edges. In this network, nodes represent servers and edges represent bidirectional communication links. To ensure the network is resilient — that is, the entire network remains connected even if one node fails — we want to add the minimum number of extra edges.

In this problem, the solution is simplified: you need to determine the minimum number of extra edges required to connect all isolated components of the network. If there are k connected components, then you need at least \(k-1\) edges to connect the network.

Note that the original network may already be connected or even resilient, in which case no additional edge is needed.

inputFormat

The input is given via standard input in the following format:

The first line contains two integers (n) and (m), where (n) is the number of nodes and (m) is the number of edges.

Each of the next (m) lines contains two integers (u) and (v) (1-based indices), representing an edge between nodes (u) and (v).

outputFormat

Output a single integer representing the minimum number of extra edges required to connect the network so that it becomes fully connected.## sample

4 2
1 2
3 4
1

</p>