BIT sophomore primary school of computer science and technology - program design - 20. Military training diary: sleep check

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]=0 or next[i][1]=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[5], that is, 2, output 2 and mark it with the flag array. Then follow the search for next[2] 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()
{
	int m,n,t,flag[100006]={0}, next[100006][2]={0},head[100006]={0},end[100006]={0},num[100006]={0};
	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
			{
				head[y]=end[x];
				end[y]=head[x];
			}
			else
			{
				if(next[head[x]][1]==0)      next[head[x]][1]=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 
				else if(next[head[x]][0]==0) next[head[x]][0]=head[y];
				if(next[head[y]][1]==0)      next[head[y]][1]=head[x];//Similarly, if you add a neighbor to the head of x, you have to do the same for y 
				else if(next[head[y]][0]==0) next[head[y]][0]=head[x];
				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 
		    head[x]=0;//The first box is empty 
		    end[x]=0;
		    num[x]=0;
		}
	}
	for(i=1;i<=n;i++)
	{
		if(num[i]==0) printf("0\n");
		else
		{
			printf("%d",num[i]);
			printf(" %d",head[i]);
			flag[head[i]]=1;//Whether the data stored in the flag array has been output, and the output is set to 1 
			t=head[i];
			for(j=1;j<num[i];j++)
			{
				if(next[t][0]!=0&&flag[next[t][0]]!=1) 
				{
					printf(" %d",next[t][0]);
					t=next[t][0];
					flag[t]=1;
				}
				if(next[t][1]!=0&&flag[next[t][1]]!=1)
				{
					printf(" %d",next[t][1]);
					t=next[t][1];
					flag[t]=1;
				}
			}
			printf("\n");
		}
	}
	return 0;
}

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