Disjoint Set Operations

Interview

Time limit2sMemory limit128 MB

Summary
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 00 through nn belongs to its own separate set. In other words, the initial sets are 0,1,2,dots,n\\{0\\}, \\{1\\}, \\{2\\}, \\dots, \\{n\\}.

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 nn and mm. Here, mm is the number of operations to process.

Each of the next mm lines contains one operation.

  • 0 a b: merge the set containing aa and the set containing bb.
  • 1 a b: check whether aa and bb belong to the same set.

Output

For each operation of the form 1 a b, print YES if aa and bb are in the same set, and NO otherwise. Print each answer on its own line.

Constraints

  • 1≤n≤1,000,0001 \le n \le 1\\,000\\,000
  • 1≤m≤100,0001 \le m \le 100\\,000
  • 0≤a,b≤n0 \le a, b \le n
  • aa and bb are integers.
  • aa and bb may be equal.

Examples1

  1. Example 1

    Input
    7 8
    0 1 3
    1 1 7
    0 7 6
    1 7 1
    0 3 7
    0 4 2
    0 1 1
    1 1 1
    
    Expected output
    NO
    NO
    YES