#include <bits/stdc++.h> #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000007; ll infl=1000000000000000007; ll mod=1000000007; ll mod1=998244353; const int N=300007,Q=1000007; int s[N+Q]; int r[N+Q]; bool is[N+Q]; int id[N]; int Find(int v) { if(r[v]==v) return v; return r[v]=Find(r[v]); } void Union(int u,int v) { r[u]=r[v]; is[v]|=is[u]; s[v]+=s[u]; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,q; cin>>n>>q; for(int i=1;i<=n+q;i++) { s[i]=1; r[i]=i; } for(int i=1;i<=n;i++) id[i]=i; int it=n; while(q--) { char c; int a,b; cin>>c; if(c=='+') { cin>>a>>b; a=Find(id[a]),b=Find(id[b]); if(a==b) is[a]=1; else Union(a,b); } else if(c=='-') { cin>>a; s[Find(id[a])]--; id[a]=++it; } else { cin>>a; a=Find(id[a]); if(is[a]) cout<<1; else if(s[a]==1) cout<<0; else cout<<'?'; } } 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 | #include <bits/stdc++.h> #define ll long long #define ld long double #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() using namespace std; int inf=1000000007; ll infl=1000000000000000007; ll mod=1000000007; ll mod1=998244353; const int N=300007,Q=1000007; int s[N+Q]; int r[N+Q]; bool is[N+Q]; int id[N]; int Find(int v) { if(r[v]==v) return v; return r[v]=Find(r[v]); } void Union(int u,int v) { r[u]=r[v]; is[v]|=is[u]; s[v]+=s[u]; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,q; cin>>n>>q; for(int i=1;i<=n+q;i++) { s[i]=1; r[i]=i; } for(int i=1;i<=n;i++) id[i]=i; int it=n; while(q--) { char c; int a,b; cin>>c; if(c=='+') { cin>>a>>b; a=Find(id[a]),b=Find(id[b]); if(a==b) is[a]=1; else Union(a,b); } else if(c=='-') { cin>>a; s[Find(id[a])]--; id[a]=++it; } else { cin>>a; a=Find(id[a]); if(is[a]) cout<<1; else if(s[a]==1) cout<<0; else cout<<'?'; } } return 0; } |