Disjoint Set Operations
InterviewTime limit2sMemory limit128 MB
Implement a union-find structure that merges sets and answers whether two elements share a set across a sequence of operations.
- Level
Easy3 of 10
- Topics
- Union-find
- Solved
- No attempts yet
Problem
Initially, each integer from through belongs to its own separate set. In other words, the initial sets are .
You must process two kinds of operations. One operation merges the sets containing two given elements, and the other checks whether two given elements are in the same set.
Write a program that processes all operations in order.
Input
The first line contains two integers and . Here, is the number of operations to process.
Each of the next lines contains one operation.
0 a b: merge the set containing and the set containing .1 a b: check whether and belong to the same set.
Output
For each operation of the form 1 a b, print YES if and are in the same set, and NO otherwise. Print each answer on its own line.
Constraints
- and are integers.
- and may be equal.