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 Rust (1.42.0) (judge verdict: Accepted)

use std::collections::*;
use std::*;
// template {{{
macro_rules ! get {($ ($ t : tt ) ,*; $ n : expr ) => {{let stdin = std :: io :: stdin () ; let ret = std :: io :: BufRead :: lines (stdin . lock () ) . take ($ n ) . map (| line | {let line = line . unwrap () ; let mut it = line . split_whitespace () ; _get ! (it ; $ ($ t ) ,* ) } ) . collect ::< Vec < _ >> () ; ret } } ; ($ ($ t : tt ) ,* ) => {{let mut line = String :: new () ; std :: io :: stdin () . read_line (& mut line ) . unwrap () ; let mut it = line . split_whitespace () ; _get ! (it ; $ ($ t ) ,* ) } } ; }
macro_rules ! _get {($ it : ident ; [char ] ) => {_get ! ($ it ; String ) . chars () . collect ::< Vec < _ >> () } ; ($ it : ident ; [u8 ] ) => {_get ! ($ it ; String ) . bytes () . collect ::< Vec < _ >> () } ; ($ it : ident ; usize1 ) => {$ it . next () . unwrap () . parse ::< usize > () . unwrap_or_else (| e | panic ! ("{}" , e ) ) - 1 } ; ($ it : ident ; [usize1 ] ) => {$ it . map (| s | s . parse ::< usize > () . unwrap_or_else (| e | panic ! ("{}" , e ) ) - 1 ) . collect ::< Vec < _ >> () } ; ($ it : ident ; [$ t : ty ] ) => {$ it . map (| s | s . parse ::<$ t > () . unwrap_or_else (| e | panic ! ("{}" , e ) ) ) . collect ::< Vec < _ >> () } ; ($ it : ident ; $ t : ty ) => {$ it . next () . unwrap () . parse ::<$ t > () . unwrap_or_else (| e | panic ! ("{}" , e ) ) } ; ($ it : ident ; $ ($ t : tt ) ,+ ) => {($ (_get ! ($ it ; $ t ) ) ,* ) } ; }
mod unionfind {
    pub struct Unionfind {
        par: Vec<i32>,
        group_count: usize,
    }
    impl Unionfind {
        pub fn new(n: usize) -> Self {
            Unionfind {
                par: vec![-1; n as usize],
                group_count: n as usize,
            }
        }
        fn find_root(&mut self, mut x: usize) -> usize {
            while self.par[x] >= 0 {
                x = self.par[x] as usize;
            }
            x
        }
        pub fn unite(&mut self, x: usize, y: usize) {
            let mut rx = self.find_root(x);
            let mut ry = self.find_root(y);
            if rx == ry {
                return;
            }
            if self.par[rx] > self.par[ry] {
                std::mem::swap(&mut rx, &mut ry);
            }
            self.par[rx] += self.par[ry];
            self.par[ry] = rx as i32;
            self.group_count -= 1;
        }
        pub fn is_same_group(&mut self, x: usize, y: usize) -> bool {
            self.find_root(x) == self.find_root(y)
        }
        pub fn get_group_size(&mut self, x: usize) -> usize {
            let rx = self.find_root(x);
            (-self.par[rx]) as usize
        }
        pub fn get_num_of_groups(&self) -> usize {
            self.group_count
        }
    }
}
// }}}

fn main() {
    let (n, m) = get!(usize, usize);
    let edge = get!(usize1, usize1;m);
    let mut uf = unionfind::Unionfind::new(n);
    for (a, b) in edge {
        uf.unite(a, b);
    }
    println!("{}", uf.get_num_of_groups() - 1);
}