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

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

1 lộ trình

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 nn vertices numbered from 00 to n−1n-1. Initially, the undirected graph has no edges. Process qq operations in order: add an edge or determine whether two vertices are connected.

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: add an undirected edge between uu and vv.
  • 1 u v: check whether uu and vv 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): 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.