#P3402. 【模板】可持久化并查集

【模板】可持久化并查集

Description

Given nn sets, the ii-th set initially contains only the element ii.

There are mm operations, of three types:

  • 1 a b Merge the sets containing aa and bb;
  • 2 k Revert to the state right after the kk-th operation (any of the three types counts as one operation). It is guaranteed that at least kk operations have already been performed (not counting this one). In particular, if k=0k=0, revert to the initial state;
  • 3 a b Query whether aa and bb belong to the same set; if yes, output 11, otherwise output 00.

Input Format

The first line contains two integers nn and mm.

Then follow mm lines. On each line, first read an integer optopt. If opt=2opt=2 then read an integer kk; otherwise read two integers a,ba, b, describing one operation.

Output Format

For each operation of type 33, output one line with one integer indicating the answer.

5 6
1 1 2
3 1 2
2 0
3 1 2
2 1
3 1 2
1
0
1

Hint

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 1m2×1051 \le m \le 2 \times 10^5, 1a,bn1 \le a, b \le n.

Translated by ChatGPT 5