#include <bits/stdc++.h> using namespace std; const int MAXN = 1 << 19; const int MAXQ = 1 << 20; const int NO_LAPTOP = 0; const int HAS_LAPTOP = 1; const int MAYBE_LAPTOP = 2; struct uni { int rep; int rank; uni() : rep(0), rank(0) {} uni(int x) : rep(x), rank(0) {} }; uni u[MAXN]; vector<int> g[MAXN]; int l[MAXN]; char res[MAXQ]; int uni_repr(int x) { if (u[x].rep == x) return x; u[x].rep = uni_repr(u[x].rep); return u[x].rep; } bool uni_same(int x, int y) { return (uni_repr(x) == uni_repr(y)); } void uni_join(int x, int y) { if (uni_same(x, y)) return; x = uni_repr(x); y = uni_repr(y); if (u[x].rank < u[y].rank) u[y].rep = x; else u[x].rep = y; if (u[x].rank == u[y].rank) u[y].rank++; } void dfs_ensure_laptop(int v, int p) { for (int w : g[v]) { if (w == p) continue; dfs_ensure_laptop(w, v); } u[v] = uni(v); g[v].clear(); l[v] = HAS_LAPTOP; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin >> n >> q; for (int i = 1; i <= n; ++i) { u[i] = uni(i); l[i] = NO_LAPTOP; } string rr = ""; while (q--) { char op; cin >> op; if (op == '+') { int a, b; cin >> a >> b; if (uni_same(a, b)) dfs_ensure_laptop(a, -1); else if (l[a] == HAS_LAPTOP) l[b] = HAS_LAPTOP; else if (l[b] == HAS_LAPTOP) l[a] = HAS_LAPTOP; else { uni_join(a, b); g[a].push_back(b); g[b].push_back(a); l[a] = MAYBE_LAPTOP; l[b] = MAYBE_LAPTOP; } } else if (op == '-') { int c; cin >> c; if (g[c].size() == 1) { dfs_ensure_laptop(g[c][0], c); g[c].clear(); } l[c] = NO_LAPTOP; } else { // if (op == '?) int d; cin >> d; if (l[d] == NO_LAPTOP) rr += '0'; else if (l[d] == HAS_LAPTOP) rr += '1'; else // l[d] == MAYBE_LAPTOP rr += '?'; } } cout << rr << endl; }
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | #include <bits/stdc++.h> using namespace std; const int MAXN = 1 << 19; const int MAXQ = 1 << 20; const int NO_LAPTOP = 0; const int HAS_LAPTOP = 1; const int MAYBE_LAPTOP = 2; struct uni { int rep; int rank; uni() : rep(0), rank(0) {} uni(int x) : rep(x), rank(0) {} }; uni u[MAXN]; vector<int> g[MAXN]; int l[MAXN]; char res[MAXQ]; int uni_repr(int x) { if (u[x].rep == x) return x; u[x].rep = uni_repr(u[x].rep); return u[x].rep; } bool uni_same(int x, int y) { return (uni_repr(x) == uni_repr(y)); } void uni_join(int x, int y) { if (uni_same(x, y)) return; x = uni_repr(x); y = uni_repr(y); if (u[x].rank < u[y].rank) u[y].rep = x; else u[x].rep = y; if (u[x].rank == u[y].rank) u[y].rank++; } void dfs_ensure_laptop(int v, int p) { for (int w : g[v]) { if (w == p) continue; dfs_ensure_laptop(w, v); } u[v] = uni(v); g[v].clear(); l[v] = HAS_LAPTOP; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin >> n >> q; for (int i = 1; i <= n; ++i) { u[i] = uni(i); l[i] = NO_LAPTOP; } string rr = ""; while (q--) { char op; cin >> op; if (op == '+') { int a, b; cin >> a >> b; if (uni_same(a, b)) dfs_ensure_laptop(a, -1); else if (l[a] == HAS_LAPTOP) l[b] = HAS_LAPTOP; else if (l[b] == HAS_LAPTOP) l[a] = HAS_LAPTOP; else { uni_join(a, b); g[a].push_back(b); g[b].push_back(a); l[a] = MAYBE_LAPTOP; l[b] = MAYBE_LAPTOP; } } else if (op == '-') { int c; cin >> c; if (g[c].size() == 1) { dfs_ensure_laptop(g[c][0], c); g[c].clear(); } l[c] = NO_LAPTOP; } else { // if (op == '?) int d; cin >> d; if (l[d] == NO_LAPTOP) rr += '0'; else if (l[d] == HAS_LAPTOP) rr += '1'; else // l[d] == MAYBE_LAPTOP rr += '?'; } } cout << rr << endl; } |