F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Stacks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/262144 K (Java/Others)
Total Submission(s): 1923    Accepted Submission(s): 376


Problem Description
There are $N$ stacks, numbered from $1$ to $N$. Initially, the $i$-th stack only contains a number $i$. Now there are $M$ move operations, and the $i$-th operation is to move all the numbers in stack $a_i$ to stack $b_i$. Specifically speaking, we pop all the numbers in stack $a_i$ one by one, and push them to stack $b_i$ in the order of popping (the first to be popped is the first to be pushed).

After all the $M$ operations, you should output the numbers in the stacks.
 

Input
There are multiple test cases.

For each test case, the first line contains $2$ integers $N$ and $M$ $(1\leq M \leq N \leq 10^5)$.

In the next $M$ lines, each line contains $2$ integers $a_i$ and $b_i$ $(1\leq a,b \leq N, ~ a\neq b)$, denoting an operation.

It's guaranteed that the sum of $N$ of all the test cases is no more than $2 \cdot 10^5$.
 

Output
For each test case, print $n$ lines. The $i$-th line begins with an integer $S_i$, denoting the number of numbers in the $i$-th stack, followed by $S_i$ integers, denoting the numbers in the $i$-th stack from top to bottom.
 

Sample Input
6 5 1 2 2 3 3 4 4 5 6 2
 

Sample Output
0 1 6 0 0 5 4 2 1 3 5 0
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 04:23:32, Gzip enabled