PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#DSU0000001. Tập hợp rời nhau (Disjoint Set Union)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tập hợp rời nhau (Disjoint Set Union)
Disjoint Set Union
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given vertices numbered from to . Initially, the undirected graph has no edges. Process operations in order: add an edge or determine whether two vertices are connected.
Input
The first line contains two integers . Each of the following lines contains three integers :
0 u v: add an undirected edge between and .1 u v: check whether and are connected.
Output
For each operation of type 1, print 1 if the vertices are connected and 0 otherwise, on a separate 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.