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

Bài tập thuộc lộ trình

1 lộ trình

Hợp nhóm và truy vấn (Unionfind)

Unionfind

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn elements numbered from 00 to n−1n-1, initially in separate sets. Process qq operations that merge sets or ask whether two elements belong to the same set.

Input

The first line contains two integers n,qn,q. Each of the following qq lines contains three integers t,u,vt,u,v:

  • 0 u v: merge the sets containing uu and vv.
  • 1 u v: check whether uu and vv 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): 1≤n≤1501\le n\le 150; 1≤q≤1501\le q\le 150; 0≤t≤10\le t\le 1; 0≤u,v<n0\le u,v<n.
  • Subtask 2 (30 points): 1≤n≤100001\le n\le 10000; 1≤q≤100001\le q\le 10000; 0≤t≤10\le t\le 1; 0≤u,v<n0\le u,v<n.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤q≤2000001\le q\le 200000; 0≤t≤10\le t\le 1; 0≤u,v<n0\le u,v<n.

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.