#include <iostream> #include <vector> using namespace std; class Dd { public: Dd(int nn, int mm) : n(nn), m(mm) { s.assign(n + m, 1); a.assign(n + m, 0); d.resize(n + m); for (int i = 0; i < n + m; ++i) { d[i] = i; } z = n + 1; } int find(int x) { if (d[x] == x) { return x; } int root = find(d[x]); d[x] = root; return root; } int join(int l, int r) { int lp = find(l); int rp = find(r); if (lp == rp) { a[lp]++; return lp; } if (s[lp] < s[rp]) { swap(lp, rp); } if (lp <= n) { d[lp] = z; s[z] = s[lp]; a[z] = a[lp]; lp = z; ++z; } d[rp] = lp; s[lp] += s[rp]; a[lp] += a[rp] + 1; return lp; } int remove(int x) { int xp = find(x); if (x != xp) { a[xp]--; s[xp]--; } d[x] = x; a[x] = 0; return xp; } int n, m; vector<int> s, a, d; int z; }; int main() { cin.tie(0)->sync_with_stdio(false); int n, m, l = 0; cin >> n >> m; Dd dd(n, m); string w(m,' '); for (int i = 0; i < m; ++i) { string op; cin >> op; if (op == "+") { int l, r; cin >> l >> r; //int p = dd.join(l, r); // cout << "join " << l << " " << r << " => " << p << " " << dd.s[p] << " " << dd.a[p] << endl; } else if (op == "-") { int x; cin >> x; //int p = dd.remove(x); // cout << "delete " << x << " => " << p << " " << dd.s[p] << " " << dd.a[p] << endl; } else if (op == "?") { int x; cin >> x; int p = dd.find(x); // cout << "query " << x << " => " << p << " " << dd.s[p] << " " << dd.a[p] << endl; if (dd.a[p] == dd.s[p]) { w[l] = '1'; } else if (dd.a[p] == 0) { w[l] = '0'; } else { w[l] = '?'; } l += 1; } } w.resize(l); cout << w << endl; return 0; }
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | #include <iostream> #include <vector> using namespace std; class Dd { public: Dd(int nn, int mm) : n(nn), m(mm) { s.assign(n + m, 1); a.assign(n + m, 0); d.resize(n + m); for (int i = 0; i < n + m; ++i) { d[i] = i; } z = n + 1; } int find(int x) { if (d[x] == x) { return x; } int root = find(d[x]); d[x] = root; return root; } int join(int l, int r) { int lp = find(l); int rp = find(r); if (lp == rp) { a[lp]++; return lp; } if (s[lp] < s[rp]) { swap(lp, rp); } if (lp <= n) { d[lp] = z; s[z] = s[lp]; a[z] = a[lp]; lp = z; ++z; } d[rp] = lp; s[lp] += s[rp]; a[lp] += a[rp] + 1; return lp; } int remove(int x) { int xp = find(x); if (x != xp) { a[xp]--; s[xp]--; } d[x] = x; a[x] = 0; return xp; } int n, m; vector<int> s, a, d; int z; }; int main() { cin.tie(0)->sync_with_stdio(false); int n, m, l = 0; cin >> n >> m; Dd dd(n, m); string w(m,' '); for (int i = 0; i < m; ++i) { string op; cin >> op; if (op == "+") { int l, r; cin >> l >> r; //int p = dd.join(l, r); // cout << "join " << l << " " << r << " => " << p << " " << dd.s[p] << " " << dd.a[p] << endl; } else if (op == "-") { int x; cin >> x; //int p = dd.remove(x); // cout << "delete " << x << " => " << p << " " << dd.s[p] << " " << dd.a[p] << endl; } else if (op == "?") { int x; cin >> x; int p = dd.find(x); // cout << "query " << x << " => " << p << " " << dd.s[p] << " " << dd.a[p] << endl; if (dd.a[p] == dd.s[p]) { w[l] = '1'; } else if (dd.a[p] == 0) { w[l] = '0'; } else { w[l] = '?'; } l += 1; } } w.resize(l); cout << w << endl; return 0; } |