#include <bits/stdc++.h> using namespace std; const int MX=1400100; int n,m,x,y,w[MX],p[MX],rk[MX],v[MX],e[MX]; char s[5]; int fs(int x) { if (p[x]!=x) p[x]=fs(p[x]); return p[x]; } void un(int x, int y) { if (x==y) { ++e[x]; return; } if (rk[x]>=rk[y]) { p[y]=x; v[x]+=v[y]; e[x]+=e[y]+1; if (rk[x]==rk[y]) ++rk[x]; } else { p[x]=y; v[y]+=v[x]; e[y]+=e[x]+1; } } int main() { scanf("%d%d",&n,&m); for (int i=1; i<=n; i++) { w[i]=i; p[i]=i; v[i]=1; } while (m--) { scanf("%s",s); scanf("%d",&x); if (s[0]=='+') { scanf("%d",&y); un(fs(w[x]),fs(w[y])); } else if (s[0]=='-') { y=fs(w[x]); --v[y]; --e[y]; w[x]=++n; p[n]=n; v[n]=1; } else { x=fs(w[x]); if (e[x]>=v[x]) putchar('1'); else if (e[x]) putchar('?'); else putchar('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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <bits/stdc++.h> using namespace std; const int MX=1400100; int n,m,x,y,w[MX],p[MX],rk[MX],v[MX],e[MX]; char s[5]; int fs(int x) { if (p[x]!=x) p[x]=fs(p[x]); return p[x]; } void un(int x, int y) { if (x==y) { ++e[x]; return; } if (rk[x]>=rk[y]) { p[y]=x; v[x]+=v[y]; e[x]+=e[y]+1; if (rk[x]==rk[y]) ++rk[x]; } else { p[x]=y; v[y]+=v[x]; e[y]+=e[x]+1; } } int main() { scanf("%d%d",&n,&m); for (int i=1; i<=n; i++) { w[i]=i; p[i]=i; v[i]=1; } while (m--) { scanf("%s",s); scanf("%d",&x); if (s[0]=='+') { scanf("%d",&y); un(fs(w[x]),fs(w[y])); } else if (s[0]=='-') { y=fs(w[x]); --v[y]; --e[y]; w[x]=++n; p[n]=n; v[n]=1; } else { x=fs(w[x]); if (e[x]>=v[x]) putchar('1'); else if (e[x]) putchar('?'); else putchar('0'); } } return 0; } |