PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#DSU0000002. Hợp nhóm và truy vấn (Unionfind)
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 nhóm và truy vấn (Unionfind)
Unionfind
Source: Library Checker
Version: Phuoc Hung OJ Extended
Problem Statement
There are elements numbered from to , initially in separate sets. Process operations that merge sets or ask whether two elements belong to the same set.
Input
The first line contains two integers . Each of the following lines contains three integers :
0 u v: merge the sets containing and .1 u v: check whether and belong to the same set.
Output
For each operation of type 1, print 1 if the elements belong to the same set and 0 otherwise, one answer per line.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
4 7
1 0 1
0 0 1
0 2 3
1 0 1
1 1 2
0 0 2
1 1 3
Output
0
1
0
1
Explanation
The first query is 0. Adding 0–1 and 2–3 makes (0,1) connected but not (1,2). After adding 0–2, vertices 1 and 3 become connected.