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: Wrong Answer)

#![allow(unused_imports)]

use text_io::*;
use proconio::*;
use std::collections::*;
use itertools::Itertools;
use std::process::exit;
use std::cmp::*;
use num::*;
use num::integer::Roots;
use std::str::FromStr;
use std::io::stdin;

fn main(){
    input! {
    n:usize,
    m:usize,
    c:[(usize,usize);m],
    }
    let mut uf=UnionFindTree::new(n);
    for i in 0..m {
        uf.unite(c[i].0-1,c[i].1-1);
        println!("{}",uf.union_number()-1)
    }
}














/// UnionFind構造体
pub struct UnionFindTree {
    /// 頂点`i`の親を格納する配列
    parents: Vec<usize>,
    /// 頂点`i`が親であるときのその木の頂点数
    sizes: Vec<usize>,
    /// 重み付きUnionFindを使う際の重みの格納配列
    weights: Vec<isize>,
    /// 頂点`i`が属する木がループを持っているかどうか
    has_loops: Vec<bool>,
}

impl UnionFindTree {
    /// UnionFind初期化
    /// 計算量はO(n)
    pub fn new(n: usize) -> Self {
        let parents = (0..n).collect();
        let sizes = vec![1; n];
        let weights = vec![0; n];
        let has_loops = vec![false; n];
        UnionFindTree {
            parents,
            sizes,
            weights,
            has_loops,
        }
    }

    /// 親を再帰的に求め、途中の計算結果をもとに親の書き換えを行う関数
    /// 計算量はO(a(n)))
    pub fn root(&mut self, x: usize) -> usize {
        if self.parents[x] == x {
            x
        } else {
            let tmp = self.root(self.parents[x]);
            self.weights[x] += self.weights[self.parents[x]];
            self.parents[x] = tmp;
            tmp
        }
    }

    pub fn size(&self, x: usize) -> usize {
        self.sizes[x]
    }

    pub fn has_loop(&self, x: usize) -> bool {
        self.has_loops[x]
    }

    /// 2つの頂点が同じ木に属しているかの判定
    /// `self.root()`を呼び出すため、`&mut self`を引数に取る。そのため、命名に`is_`を使っていない
    /// 計算量はO(a(n))
    pub fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }

    /// 重み付きUnionFindを考える際のUnite関数
    /// 計算量はO(a(n))
    pub fn unite_with_weight(&mut self, x: usize, y: usize, w: isize) {
        let root_x = self.root(x);
        let root_y = self.root(y);

        if self.same(x, y) {
            self.has_loops[root_x] = true;
            self.has_loops[root_y] = true;
        } else if self.sizes[root_x] >= self.sizes[root_y] {
            self.parents[root_y] = root_x;
            self.sizes[root_x] += self.sizes[root_y];
            self.weights[root_y] = -w - self.weights[y] + self.weights[x];
        } else {
            self.parents[root_x] = root_y;
            self.sizes[root_y] += self.sizes[root_x];
            self.weights[root_x] = w + self.weights[y] - self.weights[x];
        }
    }

    /// 重みを考慮しない際のUnite関数
    /// 重みとして0を与えているだけであり、計算量は同じくO(a(n))
    pub fn unite(&mut self, x: usize, y: usize) {
        self.unite_with_weight(x, y, 0);
    }

    /// 重み付きUnionFindにおいて、2つの頂点の距離を返す関数
    /// 2つの頂点が同じ木に属していない場合は`None`を返す
    pub fn diff(&mut self, x: usize, y: usize) -> Option<isize> {
        if self.same(x, y) {
            Some(self.weights[x] - self.weights[y])
        } else {
            None
        }
    }

    pub fn is_parent(&self, x: usize) -> bool {
        self.parents[x] == x
    }

    pub fn union_max(&self) -> usize{
        let max: &usize =self.sizes.iter().max().unwrap();
        *max
    }
    pub fn union_number(&self) -> usize {
        let mut gg=HashSet::new();
        for i in 0..self.parents.len() {
            gg.insert(self.parents[i]);
        }
        gg.len()
    }
}