P26148 [AtCoder] [ARC032B] Road Construction
普及
题目描述

Problem Description

In Daiku Cho's town, road construction is underway. The town has $ N $ intersections and $ M $ roads, with each road connecting two different intersections bidirectionally. Unfortunately, it is not guaranteed that one can travel from any intersection to any other using the existing roads.

Daiku Cho's construction company wants to build some roads connecting different intersections so that one can travel from any intersection to any other using the roads.

Determine the minimum number of roads needed to ensure that one can travel from any intersection to any other using the roads. If it is already possible to travel from any intersection to any other using the existing roads, output $ 0 $.

Input Format

The input is given from the standard input in the following format:

$ N $ $ M $ $ a_1 $ $ b_1 $ $ a_2 $ $ b_2 $ : $ a_M $ $ b_M $

  • The first line contains two integers $N\ (1\ ≦\ N\ ≦\ 100,000)$ and $M\ (0\ ≦\ M\ ≦\ 100,000)$, representing the number of intersections and the number of existing roads, respectively, separated by spaces.
  • The next $ M $ lines contain information about the existing roads. The $ i $-th line contains two integers $a_i,\ b_i\ (1\ ≦\ a_i\ ≦\ N,\ 1\ ≦\ b_i\ ≦\ N)$, representing the two intersections connected by the $ i $-th road, separated by spaces.
  • No road connects the same intersection. That is, for $i,j\ (1≦i,j≦M)$, if $ i≠j $, then $ a_i≠a_j $ or $ b_i≠b_j $.

Output Format

Output the minimum number of roads that need to be constructed in the first line.

Do not forget the newline at the end.

Sample Input and Output

Sample Input #1

4 2
1 2
1 3

Sample Output #1

1

Sample Input #2

6 4
1 2
2 3
1 3
5 6

Sample Output #2

2

Notes/Hints

Sample Explanation 1

Intersections $ 1 $, $ 2 $, and $ 3 $ are connected by roads, but intersection $ 4 $ is not connected. For example, building a road connecting intersections $ 1 $ and $ 4 $ would be sufficient. ![](http://arc032.contest.atcoder.jp/img/arc/032/B1.png)

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
4 2 1 2 1 3
样例输出
1