QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 1024 MB 總分: 100

#3760. Doubly Linked List Practice Problem

统计

Bobo has $n$ lists $L_1, L_2, \dots, L_n$. Initially, $L_i$ contains only the element $i$, i.e., $L_i = [i]$. He performs $m$ operations in sequence. The $i$-th operation is specified by two integers $a_i, b_i$, and each operation consists of two steps:

  1. $L_{a_i} \leftarrow \mathrm{reverse}(L_{a_i} + L_{b_i})$, where $\leftarrow$ denotes assignment, $+$ denotes list concatenation, and $\mathrm{reverse}$ denotes list reversal. For example, $\mathrm{reverse}([1, 2] + [3, 4, 5]) = [5, 4, 3, 2, 1]$.
  2. $L_{b_i} \leftarrow []$, where $[]$ denotes an empty list.

Output the elements of $L_1$ after $m$ operations.

Input

The input contains multiple test cases. Process until the end of the file.

The first line of each test case contains two integers $n$ and $m$.

The next $m$ lines each contain two integers $a_i, b_i$.

  • $1 \leq n, m \leq 10^5$
  • $1 \leq a_i, b_i \leq n, a_i \neq b_i$
  • The sum of $n$ and the sum of $m$ over all test cases do not exceed $5 \times 10^5$.

Output

For each test case, first output the length of $L_1$, denoted as $|L_1|$, followed by $|L_1|$ integers representing the elements of $L_1$.

Examples

Input 1

2 1
1 2
2 1
2 1
3 3
3 2
3 2
1 3

Output 1

2 2 1
0
3 2 3 1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.