这题一眼lct,然而
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; const int maxn=100010; int n,m,k,fa[maxn],sta1[maxn*50],sta2[maxn*50],top,tt,ans[maxn]; int getfa(int x){ if(x!=fa[x]){ sta1[++top]=x;sta2[top]=fa[x]; fa[x]=getfa(fa[x]); } return fa[x]; } struct edg{ int x,y,tim; }e[maxn*2]; struct que{ int num,c[5]; }q[maxn]; void cdq(int l,int r){ int now=top; if(l==r){ ans[l]=1; for(int i=1;i<=q[l].num;++i){ int fx=getfa(e[q[l].c[i]].x); int fy=getfa(e[q[l].c[i]].y); if(fx!=fy){ ans[l]=0;break; } } while(top!=now)fa[sta1[top]]=sta2[top],top--; return; } ++tt; int mid=l+r>>1; for(int i=l;i<=mid;++i) for(int j=1;j<=q[i].num;++j){ e[q[i].c[j]].tim=tt; } for(int i=mid+1;i<=r;++i) for(int j=1;j<=q[i].num;++j) if(e[q[i].c[j]].tim!=tt){ int fx=getfa(e[q[i].c[j]].x); int fy=getfa(e[q[i].c[j]].y); if(fx!=fy){ sta1[++top]=fx;sta2[top]=fa[fx]; fa[fx]=fy; } } cdq(l,mid); while(top!=now){fa[sta1[top]]=sta2[top];top--;} ++tt; for(int i=mid+1;i<=r;++i) for(int j=1;j<=q[i].num;++j){ e[q[i].c[j]].tim=tt; } for(int i=l;i<=mid;++i) for(int j=1;j<=q[i].num;++j) if(e[q[i].c[j]].tim!=tt){//把后面的所有边中前面没删的加上; int fx=getfa(e[q[i].c[j]].x); int fy=getfa(e[q[i].c[j]].y); if(fx!=fy){ sta1[++top]=fx;sta2[top]=fa[fx]; fa[fx]=fy; } } cdq(mid+1,r); } int main(){ cin>>n>>m; for(int i=1;i<=n;++i)fa[i]=i; for(int i=1;i<=m;++i){ scanf("%d%d",&e[i].x,&e[i].y); e[i].tim=0; } cin>>k;tt=1; for(int i=1;i<=k;++i){ scanf("%d",&q[i].num); for(int j=1;j<=q[i].num;++j){ scanf("%d",&q[i].c[j]); e[q[i].c[j]].tim=tt; } } for(int i=1;i<=m;++i) if(e[i].tim!=tt){ int fx=getfa(e[i].x); int fy=getfa(e[i].y); if(fx!=fy)fa[fx]=fy; } cdq(1,k); for(int i=1;i<=k;++i){ if(ans[i])puts("Connected"); else puts("Disconnected"); } return 0; }
题解说可以cdq+并查集,于是复习了一下cdq;