Problem:
Score : 300 points

Problem Statement

There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M.
Road i connects City A_i and City B_i.

Snuke can perform the following operation zero or more times:

Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities.

After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times).

What is the minimum number of roads he must build to achieve the goal?

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 100,000

1 \leq A_i < B_i \leq N

No two roads connect the same pair of cities.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

Print the answer.

Sample Input 1

3 1
1 2

Sample Output 1

1

Initially, there are three cities, and there is a road between City 1 and City 2.

Snuke can achieve the goal by building one new road, for example, between City 1 and City 3.
After that,

We can travel between 1 and 2 directly.

We can travel between 1 and 3 directly.

We can travel between 2 and 3 by following both roads (2 - 1 - 3).

Solution in Java (OpenJDK 11.0.6) (judge verdict: Accepted)

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
    // each travelled node is marked true (don't check below again)
        //a queue to check each time ki kitne hogye...
        //then a loop to find the niggas unconnected
        // loop stops at unconnected=0;
    public static int  chotumc(int p,boolean[] arr)
    {for(int i=p;i<arr.length;i++)
        if(!arr[i]) return i;
        return -1;
    }
    
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner st=new Scanner(System.in);
        int n=st.nextInt();
        int m=st.nextInt();
         ArrayList<Integer>[] arr = new ArrayList[n]; 
  
        // initializing 
        for (int i = 0; i < n; i++) { 
            arr[i] = new ArrayList<Integer>(); 
        } 
        boolean[] trav= new boolean[n];
        for (int i = 0; i < n; i++) { 
           trav[i]=false;
        } 
        
        // each travelled node is marked true (don't check below again)
        //a queue to check each time ki kitne hogye...
        //then a loop to find the niggas unconnected
        // loop stops at unconnected=0;
        for(int i =0;i<m;i++)
        {int a=st.nextInt();
        int b=st.nextInt();
            arr[a-1].add(b-1); 
        arr[b-1].add(a-1); 
        }
        int p=0,res=0;
        int curr=0;
        int tot=-1;
        Queue<Integer> q 
            = new LinkedList<>(); 
        
        
            
        while(res!=-1)
        { // here to implement the queue and mark the few)
        trav[res]=true;
        for(int j=0;j<arr[res].size();j++)
            {//ek node k saare relatives ko add krre
            if(trav[arr[res].get(j)]==false)
            {q.add(arr[res].get(j));
            trav[arr[res].get(j)]=true;}}
            
    // each travelled node is marked true (don't check below again)
        //a queue to check each time ki kitne hogye...
        //then a loop to find the niggas unconnected
        // loop stops at unconnected=0;
            
            while(q.peek()!=null)
            {curr = q.poll();
            for(int j=0;j<arr[curr].size();j++)
                {//ek node k saare relatives ko add krre
                if(trav[arr[curr].get(j)]==false)
                {q.add(arr[curr].get(j));
                trav[arr[curr].get(j)]=true;}
                }
                
           }
            //ek bar ka hogya sara... now we gotta add the first unconnected element and start the drill;
            
            
            
          
          
          res=chotumc(p,trav);
          p=res; tot++;
          //loop will repeat
        } 
        
        
        
        
        
      System.out.println(tot);
            
        
        
    }
}