#include<bits/stdc++.h> using namespace std; /* */ # define int int64_t const int64_t mod = 1e9+7; const int maxn = 1e6+10; int rep[maxn]; int s[maxn]; int sc[maxn]; bool can_have[maxn]; bool was_deleted[maxn]; int n, q; int fu_find(int s) { int cn = s; while (rep[cn] != cn) { cn = rep[cn]; } int r = cn; cn = s; while (rep[cn] != cn) { int ncn = rep[cn]; rep[cn] = r; cn = ncn; } return r; } void fu_union(int a, int b) { a = fu_find(a); b = fu_find(b); if (a == b) return; s[a] += s[b]; sc[a] += sc[b]; rep[b] = a; } void add_conn(int u, int v) { fu_union(u, v); if (u != v) { if (was_deleted[u] && was_deleted[v]) { s[fu_find(u)] += 2; } else if (was_deleted[u] || was_deleted[v]) { s[fu_find(u)] += 1; } } sc[fu_find(u)] += 1; can_have[u] = true; can_have[v] = true; was_deleted[u] = false; was_deleted[v] = false; } void del_comp(int u) { can_have[u] = false; was_deleted[u] = true; } char ask(int u) { //cout << "ask " << u << endl; if (!can_have[u]) { return '0'; } int r = fu_find(u); //cout << sc[r] <<" "<< s[r] << endl; if (sc[r] >= s[r]) { return '1'; } else { return '?'; } } void init() { for (int i = 1; i <= n; i++) { s[i] = 1; sc[i] = 0; rep[i] = i; can_have[i] = false; was_deleted[i] = false; } } void tc() { cin >> n >> q; init(); while (q--) { char c; cin >> c; if (c == '+') { int u, v; cin >> u >> v; add_conn(u, v); } else if (c == '-') { int x; cin >> x; del_comp(x); } else { int x; cin >> x; cout << ask(x) << ""; } } cout << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); tc(); return 0; int T; cin >> T; while (T--) tc(); 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 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 133 134 135 136 137 138 139 140 141 142 143 144 145 | #include<bits/stdc++.h> using namespace std; /* */ # define int int64_t const int64_t mod = 1e9+7; const int maxn = 1e6+10; int rep[maxn]; int s[maxn]; int sc[maxn]; bool can_have[maxn]; bool was_deleted[maxn]; int n, q; int fu_find(int s) { int cn = s; while (rep[cn] != cn) { cn = rep[cn]; } int r = cn; cn = s; while (rep[cn] != cn) { int ncn = rep[cn]; rep[cn] = r; cn = ncn; } return r; } void fu_union(int a, int b) { a = fu_find(a); b = fu_find(b); if (a == b) return; s[a] += s[b]; sc[a] += sc[b]; rep[b] = a; } void add_conn(int u, int v) { fu_union(u, v); if (u != v) { if (was_deleted[u] && was_deleted[v]) { s[fu_find(u)] += 2; } else if (was_deleted[u] || was_deleted[v]) { s[fu_find(u)] += 1; } } sc[fu_find(u)] += 1; can_have[u] = true; can_have[v] = true; was_deleted[u] = false; was_deleted[v] = false; } void del_comp(int u) { can_have[u] = false; was_deleted[u] = true; } char ask(int u) { //cout << "ask " << u << endl; if (!can_have[u]) { return '0'; } int r = fu_find(u); //cout << sc[r] <<" "<< s[r] << endl; if (sc[r] >= s[r]) { return '1'; } else { return '?'; } } void init() { for (int i = 1; i <= n; i++) { s[i] = 1; sc[i] = 0; rep[i] = i; can_have[i] = false; was_deleted[i] = false; } } void tc() { cin >> n >> q; init(); while (q--) { char c; cin >> c; if (c == '+') { int u, v; cin >> u >> v; add_conn(u, v); } else if (c == '-') { int x; cin >> x; del_comp(x); } else { int x; cin >> x; cout << ask(x) << ""; } } cout << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); tc(); return 0; int T; cin >> T; while (T--) tc(); return 0; } |