#C10302. Minimum Direct Connections for Network Communication

    ID: 39493 Type: Default 1000ms 256MiB

Minimum Direct Connections for Network Communication

Minimum Direct Connections for Network Communication

You are given a network of n servers and m existing direct connections between them. Two servers can communicate if there is a path (direct or indirect) connecting them. Your task is to determine the minimum number of additional direct connections required so that all servers are connected.

Formally, let the network consist of k disconnected connected components. To connect all components, you need at least $k-1$ new direct connections. For example, if there are 4 servers and the connections are: (1, 2) and (3, 4), then there are 2 components. Thus, the minimum number of additional connections required is $2-1 = 1$.

inputFormat

The input is read from standard input (stdin) and is formatted as follows:

  • The first line contains two integers n and m, where n represents the number of servers and m represents the number of existing direct connections.
  • The next m lines each contain two integers u and v, indicating that there is a direct connection between server u and server v.

outputFormat

Output to standard output (stdout) a single integer: the minimum number of additional direct connections needed to ensure that all servers can communicate with each other.

## sample
4 2
1 2
3 4
1

</p>