PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#DSU0000003. Hợp tập hợp (Union-Find)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Hợp tập hợp (Union-Find)
Union-Find
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
There are elements numbered from to . Initially, each element forms a separate set. Process operations that merge sets and ask whether two elements belong to the same set.
Input
The first line contains two integers . Each of the next lines has one of the following forms:
= a b: merge the sets containing and .? a b: check whether and belong to the same set.
Output
For each ? query, print yes if the elements belong to the same set and no otherwise, one answer per line.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
10 4
? 1 3
= 1 8
= 3 8
? 1 3
Output
no
yes
Explanation
Initially 1 and 3 are separate. Merging 1–8 and 3–8 puts all three into one set, giving yes.