PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#DSU0000016. Trượt băng (Ice Skating)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Trượt băng (Ice Skating)
Ice Skating
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are snow piles at distinct integer coordinates on an ice field. You can skate directly between two piles if they share the same -coordinate or the same -coordinate. You may add new piles at integer coordinates.
Find the minimum number of new piles needed so that every pile can be reached from every other pile.
Input
The first line contains an integer . Each of the next lines contains two integers , the coordinates of a snow pile.
Output
Print the minimum number of new piles required.
Subtasks
- Subtask 1 (20 points): ; ; all positions are distinct.
- Subtask 2 (30 points): ; ; all positions are distinct.
- Subtask 3 (50 points): ; ; all positions are distinct.
Examples
Input
2
2 1
1 2
Output
1
Explanation
The two drifts share neither a row nor a column. Adding (2,2) connects both, so the answer is 1.