#include<bits/stdc++.h> #define LL long long #define LLL __int128 #define uint unsigned #define ldb long double #define uLL unsigned long long using namespace std; const int N=2e6+5; int n,q,id[N],fd[N],s[N],sz[N]; inline int find(const int&x){ return x==fd[x]?x:fd[x]=find(fd[x]); } signed main(){ cin.tie(0)->sync_with_stdio(0); cin>>n>>q; for(int i=1;i<=n;++i)id[i]=i,fd[i]=i,s[i]=1,sz[i]=1; for(int i=1;i<=q;++i){ char o;int x;cin>>o>>x; if(o=='+'){ int y;cin>>y; x=find(id[x]),y=find(id[y]); if(x==y)--s[x];else s[x]+=s[y]-1,sz[x]+=sz[y],fd[y]=x; } else if(o=='-'){ --sz[find(id[x])],id[x]=++n,fd[n]=n,sz[n]=1,s[n]=1; } else x=find(id[x]),cout<<(s[x]==0?"1":sz[x]==1?"0":"?"); } 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 | #include<bits/stdc++.h> #define LL long long #define LLL __int128 #define uint unsigned #define ldb long double #define uLL unsigned long long using namespace std; const int N=2e6+5; int n,q,id[N],fd[N],s[N],sz[N]; inline int find(const int&x){ return x==fd[x]?x:fd[x]=find(fd[x]); } signed main(){ cin.tie(0)->sync_with_stdio(0); cin>>n>>q; for(int i=1;i<=n;++i)id[i]=i,fd[i]=i,s[i]=1,sz[i]=1; for(int i=1;i<=q;++i){ char o;int x;cin>>o>>x; if(o=='+'){ int y;cin>>y; x=find(id[x]),y=find(id[y]); if(x==y)--s[x];else s[x]+=s[y]-1,sz[x]+=sz[y],fd[y]=x; } else if(o=='-'){ --sz[find(id[x])],id[x]=++n,fd[n]=n,sz[n]=1,s[n]=1; } else x=find(id[x]),cout<<(s[x]==0?"1":sz[x]==1?"0":"?"); } return 0; } /* */ |