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
| signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n;
int idx = 0; std::vector<int> l(n + 1), r(n + 1), e(n + 1); r[0] = 1, l[1] = 0; idx = 2; std::unordered_map<int, int> h; for (int i = 0; i < n; i++) { int op; std::cin >> op;
int x, y; if (op == 1) { std::cin >> x >> y; auto add = [&](int a, int x) { h[x] = idx; e[idx] = x; l[idx] = a, r[idx] = r[a]; l[r[a]] = idx, r[a] = idx++; }; if (y == 0) { add(0, x); } else { add(h[y], x); } } else { std::cin >> x; auto remove = [&](int x) { l[r[x]] = l[x]; r[l[x]] = r[x]; }; remove(h[x]); } }
int ans = 0; for (int i = r[0]; i != 1; i = r[i]) { ans++; } std::cout << ans << "\n"; for (int i = r[0]; i != 1; i = r[i]) { std::cout << e[i] << " \n"[i == 1]; }
return 0; }
|