# 1, Title

The military training of the small army is in the middle. Today, the military training instructor made a wave of sudden attacks and conducted a bedroom investigation.

Xiao Jun, who learned the news of checking his bedroom in advance, is ready to carry out a wave of sorting and induction to make his bedroom more tidy. Specifically, Xiaojun has n items in n boxes, and the i-th box has item I. Xiaojun will sort out m times. For the i-th sorting, Xiaojun will take the items from the top of the x-th box and put them into the y-th box until the x-th box is completely empty. For example, the first box has items 1 and 2 from top to bottom, and the second box has items 3. After moving the items in box 1 into box 2, the result is: the first box has no items, and the second box has items 2, 1 and 3 from top to bottom

Now, Xiao Jun tells you what the n and m operations are. Can you tell him how many items are in each box and what they are?

Input:

A positive integer n represents the number of boxes and items, and a positive integer m represents the number of sorting and induction

Next, input m lines, one line of two positive integers x, y, representing moving the items of box x into bo x y in the above method

1≤n≤10^5, 1≤m≤10^6, 1≤x,y≤n

Title Guarantee X= y

Output;

There are n lines of output

In line I, first output a positive integer k to represent the number of items in the i-th box, and then output n numbers to represent the top-down item label of the i-th box

be careful:

There is no space at the end of the line, and there is a carriage return at the end of the text. # 2, AC code and notes

Considering the order, we will use the linked list, which is very troublesome. Therefore, we do not remember the order of the objects in the box. We directly input, operate and store the numbers of the first and last objects in each box, and store the numbers of each object's neighbors in n objects (the order of the two neighbor numbers is also not required). In this way, the principle of output changes from the sequential output of the objects in the box to the first output of the first object, and then check whether the neighbors of each object have been output. If not, output.

If you don't understand, for example. For example, the final result is that the order in box 1 is 5,2,4,1,3, but the computer does not know this order. It only knows that the top of box 1 is 5, the tail is 3, and the neighbors of each object. Since we initialize the next stored in the neighborhood, all elements are 0, so an object   i   When there is only one neighbor, it can be next[i]=0 or next[i]=0, and only the first and last objects are in this case. Therefore, from the beginning, find another number other than 0 in the next, that is, 2, output 2 and mark it with the flag array. Then follow the search for next and look at the two neighbors of 2. One must be 5 and the other 4. At this time, if you look at the flag array, you can know that 5 has been output and 4 has not, so the next neighbor of 2 is 4... And so on

```#include <stdio.h>
#include <stdlib.h>

int main()
{
scanf("%d %d",&n,&m);
int i,j,x,y;
for(i=1;i<=n;i++)//In order to facilitate thinking, we choose n groups of data from 1 to n instead of 0 to n-1
{
head[i]=i; end[i]=i; num[i]=1;//At the beginning, there was an item in each box. What was the number of the first box
}
for(i=0;i<m;i++)
{
scanf("%lld %lld",&x,&y);
if(num[x]==0) continue;//If the first box of each input is empty, skip it directly
else
{
if(num[y]==0)//If the second box is empty, just pour it in. It's easy to understand
{
}
else
{
if(next[head[x]]==0)      next[head[x]]=head[y]; //The head object in the first box has only one neighbor, so look at which of the two empty positions in the next and put the head object of y in it
head[y]=end[x];// The new head of the second box is the original end of the first box
}
num[y]+=num[x];//Don't forget to update the number of objects
end[x]=0;
num[x]=0;
}
}
for(i=1;i<=n;i++)
{
if(num[i]==0) printf("0\n");
else
{
printf("%d",num[i]);
flag[head[i]]=1;//Whether the data stored in the flag array has been output, and the output is set to 1
for(j=1;j<num[i];j++)
{
if(next[t]!=0&&flag[next[t]]!=1)
{
printf(" %d",next[t]);
t=next[t];
flag[t]=1;
}
if(next[t]!=0&&flag[next[t]]!=1)
{
printf(" %d",next[t]);
t=next[t];
flag[t]=1;
}
}
printf("\n");
}
}
return 0;
}```

Posted on Tue, 23 Nov 2021 17:31:53 -0500 by php_man555