#P3367. 【模板】并查集

【模板】并查集

Description

As stated, there is a disjoint set union (union-find). You need to perform union and query operations.

Input Format

The first line contains two integers N,MN, M, representing NN elements and MM operations.

The next MM lines each contain three integers Zi,Xi,YiZ_i, X_i, Y_i.

When Zi=1Z_i = 1, union the sets containing XiX_i and YiY_i.

When Zi=2Z_i = 2, output whether XiX_i and YiY_i are in the same set. Output Y if yes; otherwise output N.

Output Format

For each operation with Zi=2Z_i = 2, output one line containing a single uppercase letter, either Y or N.

4 7
2 1 2
1 1 2
2 1 2
1 3 4
2 1 4
1 2 3
2 1 4
N
Y
N
Y

Hint

For 15%15\% of the testdata, N10N \le 10, M20M \le 20.

For 35%35\% of the testdata, N100N \le 100, M103M \le 10^3.

For 50%50\% of the testdata, 1N1041 \le N \le 10^4, 1M2×1051 \le M \le 2 \times 10^5.

For 100%100\% of the testdata, 1N2×1051 \le N \le 2 \times 10^5, 1M1061 \le M \le 10^6, 1Xi,YiN1 \le X_i, Y_i \le N, Zi{1,2}Z_i \in \{ 1, 2 \}.

Translated by ChatGPT 5