#include <bits/stdc++.h> #define pb push_back using namespace std; int find(int x, vector<int>&link) { while(x != link[x]) x = link[x]; return x; } bool same(int a, int b, vector<int>&link) { return find(a, link) == find(b, link); } void unite(int a, int b, vector<int>&link, vector<int>&size, vector<int>&real) { a = find(a, link); b = find(b, link); if(size[a] < size[b]) swap(a, b); size[a] += size[b]; real[a] += real[b]; link[b] = a; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; //vector<tuple<int, int, int>> v; vector<int> link; vector<int> size; vector<int> stan; // 0, 1, 5 vector<int> real; vector<int> curr; for(int i = 0; i <= n; i++) { link.pb(i); size.pb(1); stan.pb(0); real.pb(1); curr.pb(i); } for(int i = 0; i < q; i++) { char z; cin >> z; if(z == '+') { int a, b; cin >> a >> b; int a1 = curr[a], b1 = curr[b]; int k1 = find(a1, link); int k2 = find(b1, link); if(stan[k1] == 1) stan[k2] = 1; else if(stan[k2] == 1) stan[k1] = 1; else if(same(a1, b1, link)) { stan[k1] = 1; } else { unite(a1, b1, link, size, real); int k3 = find(a1, link); stan[k3] = 5; } } else if(z == '-') { int c; cin >> c; int c1 = curr[c]; int k = find(c1, link); real[k]--; if(stan[k] == 5 && real[k] == 1) stan[k] = 0; int nowy = link.size(); link.pb(nowy); size.pb(1); stan.pb(0); real.pb(1); curr[c] = nowy; } else { int d; cin >> d; int d1 = curr[d]; int k = find(d1, link); if(stan[k] == 0) cout << 0; else if(stan[k] == 1) cout << 1; else cout << "?"; } } 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 | #include <bits/stdc++.h> #define pb push_back using namespace std; int find(int x, vector<int>&link) { while(x != link[x]) x = link[x]; return x; } bool same(int a, int b, vector<int>&link) { return find(a, link) == find(b, link); } void unite(int a, int b, vector<int>&link, vector<int>&size, vector<int>&real) { a = find(a, link); b = find(b, link); if(size[a] < size[b]) swap(a, b); size[a] += size[b]; real[a] += real[b]; link[b] = a; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; //vector<tuple<int, int, int>> v; vector<int> link; vector<int> size; vector<int> stan; // 0, 1, 5 vector<int> real; vector<int> curr; for(int i = 0; i <= n; i++) { link.pb(i); size.pb(1); stan.pb(0); real.pb(1); curr.pb(i); } for(int i = 0; i < q; i++) { char z; cin >> z; if(z == '+') { int a, b; cin >> a >> b; int a1 = curr[a], b1 = curr[b]; int k1 = find(a1, link); int k2 = find(b1, link); if(stan[k1] == 1) stan[k2] = 1; else if(stan[k2] == 1) stan[k1] = 1; else if(same(a1, b1, link)) { stan[k1] = 1; } else { unite(a1, b1, link, size, real); int k3 = find(a1, link); stan[k3] = 5; } } else if(z == '-') { int c; cin >> c; int c1 = curr[c]; int k = find(c1, link); real[k]--; if(stan[k] == 5 && real[k] == 1) stan[k] = 0; int nowy = link.size(); link.pb(nowy); size.pb(1); stan.pb(0); real.pb(1); curr[c] = nowy; } else { int d; cin >> d; int d1 = curr[d]; int k = find(d1, link); if(stan[k] == 0) cout << 0; else if(stan[k] == 1) cout << 1; else cout << "?"; } } cout << "\n"; return 0; } |