Problem10309--CF EDU - DSU - Step 3 - A. DSU with rollback

10309: CF EDU - DSU - Step 3 - A. DSU with rollback

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 512 MiB

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 description of queries, one per line. For a union query the line looks like "union u v" (1≤u,v≤n). For the creation of a checkpoint query the line looks like "persist". For a rollback query the like looks like "rollback". It is guaranteed that for each rollback query there exists the save point that is not yet removed.

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

HINT

CF DSU.

Source/Category