atcoder#ABC225D. [ABC225D] Play Train

[ABC225D] Play Train

Score : 400400 points

Problem Statement

Takahashi is playing with toy trains, connecting and disconnecting them. There are NN toy train cars, with car numbers: Car 11, Car 22, \ldots, Car NN. Initially, all cars are separated.

You will be given QQ queries. Process them in the order they are given. There are three kinds of queries, as follows.

  • 1 x y: Connect the front of Car yy to the rear of Car xx. It is guaranteed that:
    • xyx \neq y
    • just before this query, no train is connected to the rear of Car xx;
    • just before this query, no train is connected to the front of Car yy;
    • just before this query, Car xx and Car yy belong to different connected components.
  • 2 x y: Disconnect the front of Car yy from the rear of Car xx. It is guaranteed that:
    • xyx \neq y;
    • just before this query, the front of Car yy is directly connected to the rear of Car xx.
  • 3 x: Print the car numbers of the cars belonging to the connected component containing Car xx, from front to back.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 1Q1051 \leq Q \leq 10^5
  • 1xN1 \leq x \leq N
  • 1yN1 \leq y \leq N
  • All values in input are integers.
  • All queries satisfy the conditions in the Problem Statement.
  • The queries of the format 3 x ask to print at most 10610^6 car numbers in total.

Input

Input is given from Standard Input in the following format:

NN QQ

query1\mathrm{query}_1

query2\mathrm{query}_2

\vdots

queryQ\mathrm{query}_Q

The ii-th query queryi\mathrm{query}_i begins with an integer cic_i (11, 22, or 33) representing the kind of the query, followed by xx and yy if ci=1c_i = 1 or 22, and followed by xx if ci=3c_i = 3.

In short, each query is in one of the following three formats:

11 xx yy

22 xx yy

33 xx

Output

If a query with ci=3c_i = 3 asks to print the values j1,j2,,jMj_1, j_2, \ldots, j_M, output the following line:

MM j1j_1 j2j_2 \ldots jMj_M

Your output should consist of qq lines, where qq is the number of queries with ci=3c_i = 3. The kk-th line (1kq)(1 \leq k \leq q) should contain the response to the kk-th such query.

7 14
1 6 3
1 4 1
1 5 2
1 2 7
1 3 5
3 2
3 4
3 6
2 3 5
2 4 1
1 1 5
3 2
3 4
3 6
5 6 3 5 2 7
2 4 1
5 6 3 5 2 7
4 1 5 2 7
1 4
2 6 3

The figure below shows the cars when the first 55 queries are processed. For example, Car 22 belongs to the same connected component as Cars 3,5,6,73, 5, 6, 7, which is different from the connected component containing Cars 1,41, 4.

The figure below shows the cars when the first 1111 queries are processed.