PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000006. Kết nối các thành phố (Connect Cities)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Kết nối các thành phố (Connect Cities)

Connect Cities

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn cities and mm bidirectional roads. Some cities may not be reachable from others. Find the minimum number of additional roads needed to connect all cities.

Input

The first line contains two integers n,mn,m. Each of the next mm lines contains two integers a,ba,b, describing a bidirectional road between cities aa and bb. Cities are numbered from 11 to nn.

Output

Print the minimum number of additional roads required.

Subtasks

  • Subtask 1 (20 points): 2≤n≤1502\le n\le 150; 1≤m≤1701\le m\le 170; 1≤a,b≤n1\le a,b\le n; a≠ba\ne b; no repeated edges.
  • Subtask 2 (30 points): 2≤n≤100002\le n\le 10000; 1≤m≤60001\le m\le 6000; 1≤a,b≤n1\le a,b\le n; a≠ba\ne b; no repeated edges.
  • Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000; 1≤m≤1000001\le m\le 100000; 1≤a,b≤n1\le a,b\le n; a≠ba\ne b; no repeated edges.

Examples

Input

4 2
1 2
3 4

Output

1

Explanation

The cities form two components {1,2} and {3,4}; a single new road connects them.