題目鏈接:https://vjudge.net/contest/375518#problem/M
Given (n) initially empty stacks, there are three types of operations:
1 s v: Push the value (v) onto the top of the (s)-th stack.
2 s: Pop the topmost value out of the (s)-th stack, and print that value. If the (s)-th stack is empty, pop nothing and print “EMPTY” (without quotes) instead.
3 s t: Move every element in the (t)-th stack onto the top of the (s)-th stack in order.
Precisely speaking, denote the original size of the (s)-th stack by (S(s)), and the original size of the (t)-th stack by (S(t)). Denote the original elements in the (s)-th stack from bottom to top by (E(s,1), E(s,2), \dots, E(s,S(s))), and the original elements in the (t)-th stack from bottom to top by (E(t,1), E(t,2), \dots, E(t,S(t))).
After this operation, the (t)-th stack is emptied, and the elements in the (s)-th stack from bottom to top becomes (E(s,1), E(s,2), \dots, E(s,S(s)), E(t,1), E(t,2), \dots, E(t,S(t))). Of course, if (S(t) = 0), this operation actually does nothing.
There are (q) 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 (T), indicating the number of test cases. For each test case:
The first line contains two integers (n) and (q) ((1 \le n, q \le 3 \times 10^5)), indicating the number of stacks and the number of operations.
The first integer of the following (q) lines will be (op) ((1 \le op \le 3)), indicating the type of operation.
If (op = 1), two integers (s) and (v) ((1 \le s \le n), (1 \le v \le 10^9)) follow, indicating an operation of the first type.
If (op = 2), one integer (s) ((1 \le s \le n)) follows, indicating an operation of the second type.
If (op = 3), two integers (s) and (t) ((1 \le s, t \le n), (s \ne t)) follow, indicating an operation of the third type.
It’s guaranteed that neither the sum of (n) nor the sum of (q) over all test cases will exceed (10^6).
Output
For each operation of the second type output one line, indicating the answer.
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
Sample Output
13
12
11
10
EMPTY
14
EMPTY
EMPTY
EMPTY
EMPTY
EMPTY
EMPTY
翻譯:
輸入n和q,表示n個棧,q次操作。
operator1:輸入1 s v,將元素v壓入棧s中
operator2:輸入2 s,查詢棧s的棧頂元素
operator3:輸入3 s t,將棧t接到棧s上面
分析:
將壓入不同棧的元素存入arr數組中。
爲了區分元素屬於哪一個棧,申請三個數組top,Next,bottom
top:棧頂元素在數組中的下標
Next:次棧頂元素在數組中的下標
bottom:棧低元素在數組中的下標
- 操作1
if(op==1)
{
int s,v;
scanf("%d%d",&s,&v);
arr[++cnt]=v;
if(!bottom[s])///棧爲空
bottom[s]=cnt;
Next[cnt]=top[s];
top[s]=cnt;
}
- 操作2
else if(op==2)
{
int s;
scanf("%d",&s);
if(top[s])
{
printf("%d\n",arr[top[s]]);
top[s]=Next[top[s]];
if(!top[s])///棧爲空
bottom[s]=0;
}
else
printf("EMPTY\n");
}
- 操作3
else if(op==3)
{
int s,t;
scanf("%d%d",&s,&t);///把T棧接到S棧上面
if(top[t])
{
if(!bottom[s])
bottom[s]=bottom[t];
Next[bottom[t]]=top[s];
top[s]=top[t];
}
top[t]=bottom[t]=0;///把t棧置空
}
完整代碼:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+10;
int arr[N];
int top[N];///棧頂元素在數組中的下標
int Next[N];///次棧頂
int bottom[N];///棧低
int cnt;
void init()
{
memset(top,0,sizeof(top));
memset(Next,0,sizeof(Next));
memset(bottom,0,sizeof(bottom));
cnt=0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
int n,q;
scanf("%d%d",&n,&q);
while(q--)
{
int op;
scanf("%d",&op);
if(op==1)
{
int s,v;
scanf("%d%d",&s,&v);
arr[++cnt]=v;
if(!bottom[s])///棧爲空
bottom[s]=cnt;
Next[cnt]=top[s];
top[s]=cnt;
}
else if(op==2)
{
int s;
scanf("%d",&s);
if(top[s])
{
printf("%d\n",arr[top[s]]);
top[s]=Next[top[s]];
if(!top[s])///棧爲空
bottom[s]=0;
}
else
printf("EMPTY\n");
}
else if(op==3)
{
int s,t;
scanf("%d%d",&s,&t);///把T棧接到S棧上面
if(top[t])
{
if(!bottom[s])
bottom[s]=bottom[t];
Next[bottom[t]]=top[s];
top[s]=top[t];
}
top[t]=bottom[t]=0;
}
}
}
return 0;
}