#C4774. Connecting the Islands
Connecting the Islands
Connecting the Islands
In an archipelago, there are ( n ) islands labeled from 1 to ( n ) and ( m ) existing bridges. Each bridge connects two different islands. Your task is to determine the minimum number of new bridges that must be built so that all islands become connected.
Consider the islands as nodes of an undirected graph. The existing bridges form edges between these nodes. If the graph consists of ( c ) connected components, then ( c-1 ) new bridges are required to connect the islands into a single connected component (i.e. the answer is ( c-1 )).
inputFormat
The first line of input contains two integers ( n ) and ( m ) representing the number of islands and the number of existing bridges, respectively. Each of the following ( m ) lines contains two space-separated integers ( u ) and ( v ), indicating that there is a bridge between island ( u ) and island ( v ).
outputFormat
Output one integer: the minimum number of new bridges required so that all islands are connected.## sample
4 2
1 2
3 4
1