思路:
记录一下c++ stl中的双向链表list的各种用法。
https://blog.csdn.net/fanyun_01/article/details/56881515
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 list<int> a[150005]; 5 6 int main() 7 { 8 int n, q, t, u, v, w; 9 while (scanf("%d %d", &n, &q) != EOF) 10 { 11 for (int i = 1; i <= n; i++) a[i].clear(); 12 while (q--) 13 { 14 scanf("%d", &t); 15 if (t == 1) 16 { 17 scanf("%d%d%d", &u, &w, &v); 18 if (w == 0) a[u].push_front(v); 19 else a[u].push_back(v); 20 } 21 else if (t == 2) 22 { 23 scanf("%d%d", &u, &w); 24 int ans; 25 if (a[u].empty()) ans = -1; 26 else if (w == 0) 27 { 28 ans = a[u].front(); a[u].pop_front(); 29 } 30 else 31 { 32 ans = a[u].back(); a[u].pop_back(); 33 } 34 printf("%d ", ans); 35 } 36 else 37 { 38 scanf("%d%d%d", &u, &v, &w); 39 if (w == 1) a[v].reverse(); 40 a[u].splice(a[u].end(), a[v]); 41 } 42 } 43 } 44 return 0; 45 }