P26955 [AtCoder] Connected Components
NOI
题目描述

Problem Description

There is a graph with $ N $ vertices, numbered $1, \dots, N$. Initially, there are no edges in this graph.

Process $ Q $ queries of the following format:

  • 1 u v: Add an undirected edge between vertices $ u $ and $ v $.
  • 2 u: Output all vertex numbers that can be reached from vertex $ u $ by traversing zero or more edges, in ascending order, at the time the query is given.

It is guaranteed that the total number of vertex numbers output in the queries of the form 2 u in one test case does not exceed $2 \times 10^5$.

Input Format

Input is given from standard input in the following format, where $\mathrm{Query}_i\ (1 \leq i \leq Q)$ represents the $ i $-th query given:

$ N $ $ Q $ $\mathrm{Query}_1$ $\vdots$ $\mathrm{Query}_Q$

Output Format

For each query of the form 2 u, output all vertex numbers that can be reached from vertex $ u $ by traversing zero or more edges, in ascending order, at the time the query is given. Each vertex number should be separated by a space, and a newline should be printed at the end.

Sample Input and Output

Sample Input #1

5 7
1 1 2
2 2
1 3 4
2 5
1 1 4
2 1
2 4

Sample Output #1

1 2
5
1 2 3 4
1 2 3 4

Sample Input #2

1 2
2 1
2 1

Sample Output #2

1
1

Sample Input #3

3 3
1 1 2
2 1
1 1 2

Sample Output #3

1 2

Notes/Hints

Constraints

  • $1 \leq N, Q \leq 2 \times 10^5$
  • For queries of the form 1 u v, $1 \leq u < v \leq N$
  • For queries of the form 2 u, $1 \leq u \leq N$
  • There is at least one query of the form 2 u.
  • The total number of vertex numbers output in queries of the form 2 u in one test case does not exceed $2 \times 10^5$.
  • All inputs are integers.

Sample Explanation 3

Edges connecting the same pair of vertices may be added multiple times.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
5 7 1 1 2 2 2 1 3 4 2 5 1 1 4 2 1 2 4
样例输出
1 2 5 1 2 3 4 1 2 3 4