#C9725. Minimum Edges to Connect Graph

    ID: 53850 Type: Default 1000ms 256MiB

Minimum Edges to Connect Graph

Minimum Edges to Connect Graph

You are given a graph with (N) nodes and (E) edges. The nodes are labeled from 1 to (N). The graph may be disconnected. Your task is to determine the minimum number of additional edges required to connect the graph.

In particular, if the graph has (k) connected components, the minimum number of edges needed to connect the graph is (k - 1).

Note: The additional edges can connect any two nodes from different components.

inputFormat

The input is given via standard input (stdin). The first line contains two integers (N) and (E), representing the number of nodes and the number of edges, respectively. Each of the following (E) lines contains two integers (u) and (v), indicating an undirected edge between nodes (u) and (v).

outputFormat

Print a single integer denoting the minimum number of additional edges required to make the graph connected.## sample

6 3
1 2
2 3
4 5
2