Given initially empty stacks, there are three types of operations:
- 1 s v: Push the value onto the top of the -th stack.
- 2 s: Pop the topmost value out of the -th stack, and print that value. If the -th stack is empty, pop nothing and print "EMPTY" (without quotes) instead.
- 3 s t: Move every element in the -th stack onto the top of the -th stack in order.
Precisely speaking, denote the original size of the -th stack by , and the original size of the -th stack by . Denote the original elements in the -th stack from bottom to top by , and the original elements in the -th stack from bottom to top by .
After this operation, the -th stack is emptied, and the elements in the -th stack from bottom to top becomes . Of course, if , this operation actually does nothing.
There are operations in total. Please finish these operations in the input order and print the answer for every operation of the second type.
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains two integers and (), indicating the number of stacks and the number of operations.
The first integer of the following lines will be (), indicating the type of operation.
- If , two integers and (, ) follow, indicating an operation of the first type.
- If , one integer () follows, indicating an operation of the second type.
- If , two integers and (, ) follow, indicating an operation of the third type.
It‘s guaranteed that neither the sum of nor the sum of over all test cases will exceed .
<h4< dd="">Output
For each operation of the second type output one line, indicating the answer.
<h4< dd="">Sample Input
2 2 15 1 1 10 1 1 11 1 2 12 1 2 13 3 1 2 1 2 14 2 1 2 1 2 1 2 1 2 1 3 2 1 2 2 2 2 2 2 3 7 3 1 2 3 1 3 3 2 1 2 1 2 2 2 3 2 3
<h4< dd="">Sample Output
13 12 11 10 EMPTY 14 EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
1 #include<iostream> 2 using namespace std; 3 #include<cstdio> 4 typedef long long ll; 5 struct Node{ 6 Node *pt; 7 ll v; 8 Node *pre; 9 Node(){ 10 pt = NULL; 11 } 12 }; 13 struct Stack{ 14 Node *head; 15 Node *tail; 16 Stack(){ 17 head=NULL; 18 tail=NULL; 19 } 20 void insert(ll p){ 21 if(head==NULL){ 22 head = new Node(); 23 tail = head; 24 head->v = p; 25 head->pre = NULL; 26 } 27 else{ 28 tail->pt = new Node(); 29 tail->pt->pre = tail; 30 tail = tail->pt; 31 tail->v = p; 32 } 33 } 34 void pop(){ 35 if(tail==head){ 36 tail = NULL; 37 head = NULL; 38 } 39 else{ 40 Node *temp = tail; 41 tail = tail->pre; 42 delete temp; 43 } 44 } 45 void top(){ 46 if(tail!=NULL){ 47 printf("%d\n",tail->v); 48 pop(); 49 } 50 else 51 printf("EMPTY\n"); 52 } 53 void connect(Stack &pt){ 54 if(pt.head==NULL) 55 return; 56 if(this->tail==NULL){ 57 this->head = pt.head; 58 this->tail =pt.tail; 59 pt.head = NULL; 60 pt.tail = NULL; 61 return; 62 } 63 this->tail->pt = pt.head; 64 pt.head->pre = this->tail; 65 this->tail = pt.tail; 66 pt.head = NULL; 67 pt.tail = NULL; 68 } 69 }; 70 Stack *a; 71 void deal(){ 72 int n,m; 73 scanf("%d%d",&n,&m); 74 a = new Stack[n+10]; 75 for(int i=0;i<m;i++){ 76 int p,q,r; 77 scanf("%d",&p); 78 if(p==1){ 79 scanf("%d%d",&q,&r); 80 a[q].insert(r); 81 } 82 else if(p==2){ 83 scanf("%d",&q); 84 a[q].top(); 85 } 86 else if(p==3){ 87 scanf("%d%d",&q,&r); 88 a[q].connect(a[r]); 89 } 90 } 91 } 92 int main(){ 93 int t; 94 scanf("%d",&t); 95 while(t--) 96 deal(); 97 return 0; 98 }
原文地址:https://www.cnblogs.com/xfww/p/8819536.html