题意:模拟栈,正常pop,push,多一个merge A B 形象地说就是就是将栈B堆到栈A上。
题解:直接用list 的pop_back,push_back,splice 模拟,
坑:用splice,第一次超时,正要大改 发现是cin超时。。。
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<vector> #include<cstring> #include<set> #include<algorithm> #include<stack> #include<string> #include<cstdio> #include<list> #define _for(i, a, b) for (int i = (a); i<(b); ++i) using namespace std; const int N = 3e5 + 5; const int INF = 1e6; list<int> v[N]; int main() { int t; cin >> t; while (t--) { int n, q; int a, b; cin >> n >> q; _for(i, 1, n + 1)v[i].clear(); while (q--) { int x; scanf("%d", &x); if (x == 1) { scanf("%d%d", &a, &b); v[a].push_back(b); } if (x == 2) { int b; scanf("%d", &b); if (v[b].empty())printf("EMPTY "); else { printf("%d ", v[b].back()); v[b].pop_back(); } } if (x == 3) { scanf("%d%d", &a, &b); v[a].splice(v[a].end(), v[b]); } } } //system("pause"); return 0; }