#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); i--) #define SZ(a) (int)a.size() #define ALL(a) a.begin(), a.end() #define PB push_back #define MP make_pair #define F first #define S second typedef long long LL; typedef vector<int> VI; typedef pair<int, int> PII; typedef double db; struct DSU { VI p, sz, ok; void init(int n) { p.resize(n); sz.resize(n); ok.resize(n); iota(ALL(p), 0); sz.assign(n, 1); ok.assign(n, 0); } int find(int v) { if(p[v] == v) return v; return p[v] = find(p[v]); } bool unite(int u, int v) { u = find(u); v = find(v); if(u == v) return false; if(sz[u] > sz[v]) swap(u, v); p[u] = v; ok[v] |= ok[u]; sz[v] += sz[u]; return true; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); int n, q; cin >> n >> q; DSU D; D.init(n + q); VI who(n); iota(ALL(who), 0); while(q--) { char x; cin >> x; if(x == '+') { int a, b; cin >> a >> b; a = who[a - 1]; b = who[b - 1]; if(!D.unite(a, b)) D.ok[D.find(a)] = 1; continue; } if(x == '-') { int c; cin >> c; who[c - 1] = n++; continue; } int d; cin >> d; d = who[d - 1]; d = D.find(d); if(D.ok[d]) { cout << '1'; continue; } if(D.sz[d] > 1) { cout << '?'; continue; } cout << '0'; } cout << "\n"; 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 | #include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); i--) #define SZ(a) (int)a.size() #define ALL(a) a.begin(), a.end() #define PB push_back #define MP make_pair #define F first #define S second typedef long long LL; typedef vector<int> VI; typedef pair<int, int> PII; typedef double db; struct DSU { VI p, sz, ok; void init(int n) { p.resize(n); sz.resize(n); ok.resize(n); iota(ALL(p), 0); sz.assign(n, 1); ok.assign(n, 0); } int find(int v) { if(p[v] == v) return v; return p[v] = find(p[v]); } bool unite(int u, int v) { u = find(u); v = find(v); if(u == v) return false; if(sz[u] > sz[v]) swap(u, v); p[u] = v; ok[v] |= ok[u]; sz[v] += sz[u]; return true; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); int n, q; cin >> n >> q; DSU D; D.init(n + q); VI who(n); iota(ALL(who), 0); while(q--) { char x; cin >> x; if(x == '+') { int a, b; cin >> a >> b; a = who[a - 1]; b = who[b - 1]; if(!D.unite(a, b)) D.ok[D.find(a)] = 1; continue; } if(x == '-') { int c; cin >> c; who[c - 1] = n++; continue; } int d; cin >> d; d = who[d - 1]; d = D.find(d); if(D.ok[d]) { cout << '1'; continue; } if(D.sz[d] > 1) { cout << '?'; continue; } cout << '0'; } cout << "\n"; return 0; } |