10309: CF EDU - DSU - Step 3 - A. DSU with rollback
[Creator : ]
Description
In this problem, you have to implement DSU with rollback. This data structure should support three operations:
- "union u v" — unite two sets that contain $u$ and $v$, and output the current number of disjoint sets.
- "persist" — create a checkpoint to which the structure can rollback later.
- "rollback" — rollback to the latest checkpoint, get rid of the point, and output the current number of disjoint sets.
Input
The first line of the input contains two integers $n,m\ (1≤n,m≤2⋅10^5)$ — the initial number of one-size sets and the number of queries.
Next $m$ lines contain the desc
Output
For each operation "union" and "rollback" output the current number of disjoint sets in a separate line.
Sample 1 Input
5 10
persist
union 1 2
union 3 4
persist
union 1 4
union 2 3
rollback
union 4 5
rollback
union 1 5
Sample 1 Output
4
3
2
2
3
2
5
4