-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSPOJ22553.cc
More file actions
64 lines (58 loc) · 1.28 KB
/
SPOJ22553.cc
File metadata and controls
64 lines (58 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPOJ 22553: Stack Overflow
// http://www.spoj.com/problems/PRJAN15F/
//
// Solution: trivial
#include <iostream>
#include <vector>
#include <list>
#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
void doit() {
int n, q;
scanf("%d %d", &n, &q);
vector<list<int>> piles(n+1);
for (int i = 0; i < q; ++i) {
char s[1024];
scanf("%s", s);
if (s[0] == 'p' && s[1] == 'u' && s[2] == 's') {
int k, a;
scanf("%d %d", &k, &a);
piles[k].push_back(a);
}
if (s[0] == 'p' && s[1] == 'u' && s[2] == 't') {
int k, l;
scanf("%d %d", &k, &l);
piles[k].splice(piles[k].end(), piles[l]);
}
if (s[0] == 'p' && s[1] == 'o') {
int k;
scanf("%d", &k);
if (piles[k].empty()) {
//printf("Empty!\n");
} else {
piles[k].pop_back();
}
}
if (s[0] == 't' && s[1] == 'o') {
int k;
scanf("%d", &k);
if (piles[k].empty()) {
printf("Empty!\n");
} else {
printf("%d\n", piles[k].back());
}
}
}
}
int main() {
int ncase; scanf("%d", &ncase);
for (int icase = 0; icase < ncase; ++icase) {
printf("Case %d:\n", icase+1);
doit();
}
}