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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Kết nối các thành phố (Connect Cities)
Connect Cities
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
There are cities and 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 . Each of the next lines contains two integers , describing a bidirectional road between cities and . Cities are numbered from to .
Output
Print the minimum number of additional roads required.
Subtasks
- Subtask 1 (20 points): ; ; ; ; no repeated edges.
- Subtask 2 (30 points): ; ; ; ; no repeated edges.
- Subtask 3 (50 points): ; ; ; ; 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.