#include <bits/stdc++.h> using namespace std; using lint = long long; using pi = array<lint, 2>; #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() #define cr(v, n) (v).clear(), (v).resize(n); const int MAXN = 1313131; struct disj { int pa[MAXN], sz[MAXN], inc[MAXN], fucked[MAXN]; void init(int n) { iota(pa, pa + n + 1, 0); fill(sz, sz + MAXN, 1); } int find(int x) { return pa[x] = (pa[x] == x ? x : find(pa[x])); } bool uni(int u, int v) { u = find(u); v = find(v); if (u == v) return 0; pa[u] = v; sz[v] += sz[u]; inc[v] += inc[u]; fucked[v] |= fucked[u]; return 1; } void compIncr(int v) { v = find(v); inc[v]++; } void fuckit(int v) { v = find(v); fucked[v] = 1; } bool fuck(int v) { v = find(v); return fucked[v]; } bool paidFull(int v) { v = find(v); return inc[v] >= sz[v] - 1; } } disj; int deg[MAXN], idx[MAXN], vis[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; disj.init(n + q); iota(idx, idx + n, 0); for (int i = 0; i < q; i++) { string s; cin >> s; if (s == "+") { int a, b; cin >> a >> b; a--; b--; a = idx[a]; b = idx[b]; if (a != b) { deg[a]++; deg[b]++; } if (!disj.uni(a, b)) { disj.fuckit(a); } } else if (s == "-") { int v; cin >> v; v--; disj.compIncr(idx[v]); idx[v] = n++; } else { int v; cin >> v; v--; v = idx[v]; if (disj.fuck(v)) { cout << "1"; } else if (deg[v] && !disj.paidFull(v)) { cout << "?"; } else cout << "0"; } } cout << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; using lint = long long; using pi = array<lint, 2>; #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() #define cr(v, n) (v).clear(), (v).resize(n); const int MAXN = 1313131; struct disj { int pa[MAXN], sz[MAXN], inc[MAXN], fucked[MAXN]; void init(int n) { iota(pa, pa + n + 1, 0); fill(sz, sz + MAXN, 1); } int find(int x) { return pa[x] = (pa[x] == x ? x : find(pa[x])); } bool uni(int u, int v) { u = find(u); v = find(v); if (u == v) return 0; pa[u] = v; sz[v] += sz[u]; inc[v] += inc[u]; fucked[v] |= fucked[u]; return 1; } void compIncr(int v) { v = find(v); inc[v]++; } void fuckit(int v) { v = find(v); fucked[v] = 1; } bool fuck(int v) { v = find(v); return fucked[v]; } bool paidFull(int v) { v = find(v); return inc[v] >= sz[v] - 1; } } disj; int deg[MAXN], idx[MAXN], vis[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; disj.init(n + q); iota(idx, idx + n, 0); for (int i = 0; i < q; i++) { string s; cin >> s; if (s == "+") { int a, b; cin >> a >> b; a--; b--; a = idx[a]; b = idx[b]; if (a != b) { deg[a]++; deg[b]++; } if (!disj.uni(a, b)) { disj.fuckit(a); } } else if (s == "-") { int v; cin >> v; v--; disj.compIncr(idx[v]); idx[v] = n++; } else { int v; cin >> v; v--; v = idx[v]; if (disj.fuck(v)) { cout << "1"; } else if (deg[v] && !disj.paidFull(v)) { cout << "?"; } else cout << "0"; } } cout << "\n"; } |