提交时间:2024-01-13 16:30:38

运行 ID: 24905

#include<bits/stdc++.h> using namespace std; const int MAXN=100010,Mod=1000000007; int _,n,q,cnt,ans,vis[MAXN],lstans,fa[MAXN]; struct side{ int u,v; bool operator ==(const side&G)const{ return u==G.u&&v==G.v; } }s[MAXN<<2]; int fid(int x){return fa[x]=((fa[x]==x)?x:fid(fa[x]));} void mrge(int x,int y){ int u=fid(x),v=fid(y); if(u!=v){ fa[u]=v; } } int vs[MAXN]; void slv(){ scanf("%d%d",&n,&q); for(int i=1;i<=n;i++){ int k; scanf("%d",&k); for(int j=1;j<=k;j++){ int v; scanf("%d",&v); s[++cnt]={i,v}; } } for(int i=1;i<=n;i++)fa[i]=i; for(int i=1;i<=cnt;i++){ // cout<<s[i].u<<" "<<s[i].v<<endl; mrge(s[i].u,s[i].v); } while(q--){ char opt=getchar();int x,y; while(opt!='?'&&opt!='-')opt=getchar(); scanf("%d%d",&x,&y);x^=lstans;y^=lstans; if(opt=='-'){ans=0; for(int i=1;i<=cnt;i++){ if((s[i].u==x&&s[i].v==y)||(s[i].u==y&&s[i].v==x)){ swap(s[cnt],s[i]); cnt--; i--; } } for(int i=1;i<=n;i++)fa[i]=i; for(int i=1;i<=cnt;i++){ // cout<<s[i].u<<" "<<s[i].v<<endl; mrge(s[i].u,s[i].v); }ans=0; for(int i=1;i<=n;i++){ // cout<<" "<<q+1<<" "<<fid(i)<<" "<<vs[fid(i)]<<endl; if(vs[fid(i)]!=q+1){ // cout<<q+1<<" "<<fid(i)<<" "<<vs[fid(i)]<<endl; ans++; vs[fid(i)]=q+1; } } lstans=ans; printf("%d\n",ans); } if(opt=='?'){ lstans=(bool)(fid(x)==fid(y)); printf("%d\n",lstans); } } } signed main(){ slv(); return 0; }