#abc225d. D - Play Train

D - Play Train

Score : 400400 points

问题描述

高桥正在玩玩具火车,连接和断开它们。 共有 NN 辆玩具火车车厢,编号分别为:车1、车2、……、车NN。 最初,所有车厢都是分开的。

你将收到 QQ 个查询请求,请按照给定顺序处理。查询有三种类型,如下所示:

  • 1 x y:将车yy的前端与车xx的后端连接起来。
    确保满足以下条件:

    • xyx \neq y
    • 在此查询之前,没有火车连接到车xx的后端;
    • 在此查询之前,没有火车连接到车yy的前端;
    • 在此查询之前,车xx和车yy属于不同的连通分量。
  • 2 x y:将车yy的前端从车xx的后端断开连接。
    确保满足以下条件:

    • xyx \neq y
    • 在此查询之前,车yy的前端直接连接到车xx的后端。
  • 3 x:打印包含车xx的连通分量中车厢的编号,从前到后

以上为通义千问 qwen-max 翻译,仅供参考。

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.

Sample Input 1

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

Sample Output 1

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.

update @ 2024/3/10 09:53:22