#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif vector<int> p; vector<int> cnt; vector<vector<int>> children; vector<bool> active; int find(int a){ if(p[a] != a)p[a] = find(p[a]); return p[a]; } void activate(int v){ active[v] = true; for(auto x : children[v])activate(x); p[v] = v; cnt[v] = 1; children[v].clear(); } void uni(int a, int b){ a = find(a); b = find(b); if(a == b){ //debug(a); activate(a); return; } if(cnt[a] < cnt[b])swap(a, b); p[b] = a; children[a].push_back(b); cnt[a] += cnt[b]; } int newNode(){ int id = sz(p); p.push_back(id); cnt.push_back(1); active.push_back(false); children.push_back(vector<int>()); return id; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, q; cin>>n>>q; p.resize(n); cnt.resize(n, 1); children.resize(n); active.resize(n, false); rep(i, n)p[i] = i; vector<int> id(n+1); rep(i, n)id[i+1] = i; rep(i, q){ char ch; cin>>ch; if(ch == '+'){ int a, b; cin>>a>>b; a = id[a]; b = id[b]; if(active[a]){ assert(a != b); activate(find(b)); }else if(active[b]){ assert(a != b); activate(find(a)); }else{ uni(a, b); } }else if(ch == '-'){ int c; cin>>c; cnt[find(id[c])]--; id[c] = newNode(); }else{ int d; cin>>d; if(active[id[d]]){ cout<<"1"; }else if(cnt[find(id[d])] > 1){ cout<<"?"; }else{ 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 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator<<(auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator<<(auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << __LINE__ << ": [" #x "]: ", dump(x) #else #define debug(...) 0 #endif vector<int> p; vector<int> cnt; vector<vector<int>> children; vector<bool> active; int find(int a){ if(p[a] != a)p[a] = find(p[a]); return p[a]; } void activate(int v){ active[v] = true; for(auto x : children[v])activate(x); p[v] = v; cnt[v] = 1; children[v].clear(); } void uni(int a, int b){ a = find(a); b = find(b); if(a == b){ //debug(a); activate(a); return; } if(cnt[a] < cnt[b])swap(a, b); p[b] = a; children[a].push_back(b); cnt[a] += cnt[b]; } int newNode(){ int id = sz(p); p.push_back(id); cnt.push_back(1); active.push_back(false); children.push_back(vector<int>()); return id; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, q; cin>>n>>q; p.resize(n); cnt.resize(n, 1); children.resize(n); active.resize(n, false); rep(i, n)p[i] = i; vector<int> id(n+1); rep(i, n)id[i+1] = i; rep(i, q){ char ch; cin>>ch; if(ch == '+'){ int a, b; cin>>a>>b; a = id[a]; b = id[b]; if(active[a]){ assert(a != b); activate(find(b)); }else if(active[b]){ assert(a != b); activate(find(a)); }else{ uni(a, b); } }else if(ch == '-'){ int c; cin>>c; cnt[find(id[c])]--; id[c] = newNode(); }else{ int d; cin>>d; if(active[id[d]]){ cout<<"1"; }else if(cnt[find(id[d])] > 1){ cout<<"?"; }else{ cout<<"0"; } } } cout<<"\n"; return 0; } |