#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 5e6+10; struct dsu { vector<int> p, mst, sub; void init(int sz) { p.resize(sz+10); mst.assign(sz+10, false); sub.assign(sz+10, 1); for (int i=0; i<sz+10; ++i) p[i]=i; } int f(int x) { if (x == p[x]) return x; return p[x]=f(p[x]); } void mrg(int x, int y) { x=f(x), y=f(y); if (x == y) return; sub[y]+=sub[x], mst[y]|=mst[x]; p[x]=y; } bool can(int x) { return sub[f(x)] > 1; } }; int n, q, idx[N]; void solve() { cin>>n>>q; for (int i=1; i<N; ++i) idx[i]=i; dsu d; d.init(2*(n+q)+100); int tp=(n+q+10); for (int i=1; i<=q; ++i) { char x; cin>>x; if (x == '+') { int a, b; cin>>a>>b; a=idx[a], b=idx[b]; if (a == b) { d.mrg(a, b); d.mst[d.f(a)]=true; continue; } if (d.mst[d.f(a)] || d.mst[d.f(b)]) { d.mrg(a, b); } else if (d.can(a) && d.can(b)) { if (d.f(a) == d.f(b)) { d.mst[d.f(a)]=true; } else d.mrg(a, b); } else { d.mrg(a, b); } } else if (x == '-') { int a; cin>>a; --d.sub[d.f(idx[a])]; idx[a]=tp++; } else { int a; cin>>a; if (d.mst[d.f(idx[a])]) cout<<"1"; else if (d.can(idx[a])) cout<<"?"; else cout<<"0"; } } cout<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 5e6+10; struct dsu { vector<int> p, mst, sub; void init(int sz) { p.resize(sz+10); mst.assign(sz+10, false); sub.assign(sz+10, 1); for (int i=0; i<sz+10; ++i) p[i]=i; } int f(int x) { if (x == p[x]) return x; return p[x]=f(p[x]); } void mrg(int x, int y) { x=f(x), y=f(y); if (x == y) return; sub[y]+=sub[x], mst[y]|=mst[x]; p[x]=y; } bool can(int x) { return sub[f(x)] > 1; } }; int n, q, idx[N]; void solve() { cin>>n>>q; for (int i=1; i<N; ++i) idx[i]=i; dsu d; d.init(2*(n+q)+100); int tp=(n+q+10); for (int i=1; i<=q; ++i) { char x; cin>>x; if (x == '+') { int a, b; cin>>a>>b; a=idx[a], b=idx[b]; if (a == b) { d.mrg(a, b); d.mst[d.f(a)]=true; continue; } if (d.mst[d.f(a)] || d.mst[d.f(b)]) { d.mrg(a, b); } else if (d.can(a) && d.can(b)) { if (d.f(a) == d.f(b)) { d.mst[d.f(a)]=true; } else d.mrg(a, b); } else { d.mrg(a, b); } } else if (x == '-') { int a; cin>>a; --d.sub[d.f(idx[a])]; idx[a]=tp++; } else { int a; cin>>a; if (d.mst[d.f(idx[a])]) cout<<"1"; else if (d.can(idx[a])) cout<<"?"; else cout<<"0"; } } cout<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); } |