zcmimi's blog
avatar
zc
2020-04-20 17:17:00
  • 本文总阅读量
查看原题

点击跳转

要转化为加边,之后用并查集维护是否连通就可以了(带权维护连通块大小,为n的时候说明全部连通)

每条边都在特定的时间段中出现

由于加边完还要删掉这里使用按秩合并

#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define il __inline__ __attribute__ ((always_inline))
typedef long long ll;typedef unsigned long long ull;typedef double db;typedef short sht;
#define fur(i,x,y) for(int i(x);i<=y;++i)
#define fdr(i,x,y) for(int i(x);i>=y;--i)
template <class T>T MAX(T x,T y){return x>y?x:y;}
template <class T>T MIN(T x,T y){return x<y?x:y;}
template <class T>void SWAP(T &x,T &y){T t=x;x=y;y=t;}
namespace IO{const int str=1<<20;static char in_buf[str],*in_s,*in_t;bool __=0;char gc(){return (in_s==in_t)&&(in_t=(in_s=in_buf)+fread(in_buf,1,str,stdin)),in_s==in_t?EOF:*in_s++;}void in(char &ch){if(__)return;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF)__=1;else ch=c;}void in(char *ch){*ch='\0';if(__)return;char c;while((c=gc())!=EOF&&isspace(c));if(c==EOF){__=1;return;}*ch=c;ch++;while((c=gc())!=EOF&&!isspace(c))*ch=c,ch++;if(c==EOF)__=1;*ch='\0';}template<typename T>void in(T &x){if(__)return;char c=gc();bool f=0;while(c!=EOF&&(c<'0'||c>'9'))f^=(c=='-'),c=gc();if(c==EOF){__=1;return;}x=0;while(c!=EOF&&'0'<=c&&c<='9')x=x*10+c-48,c=gc();if(c==EOF)__=1;if(f)x=-x;}template<typename T,typename ... arr>void in(T &x,arr & ... y){in(x),in(y...);}const char ln='\n';static char out_buf[str],*out_s=out_buf,*out_t=out_buf+str;void flush(){fwrite(out_buf,1,out_s-out_buf,stdout);out_s=out_buf;}void pt(char c){(out_s==out_t)?(fwrite(out_s=out_buf,1,str,stdout),*out_s++=c):(*out_s++=c);}void out(const char* s){while(*s)pt(*s++);}void out(char* s){while(*s)pt(*s++);}void out(char c){pt(c);}template<typename T>void out(T x){if(!x){pt('0');return;}if(x<0)pt('-'),x=-x;char a[50],t=0;while(x)a[t++]=x%10,x/= 10;while(t--)pt(a[t]+'0');}template<typename T,typename ... arr>void out(T x,arr & ... y){out(x),out(y...);}}using namespace IO;
using namespace std;
const int N=100011,M=200011;
int n,q,k,tp,cnt,f[N],siz[N],X[M],Y[M],head[N*4],st[N*80];
list<int>T[M];
int gf(int x){while(f[x])x=f[x];return x;}
struct edge{int to,nxt;}e[N*80];
void add(int x,int y){e[++cnt].to=y;e[cnt].nxt=head[x];head[x]=cnt;}
void upd(int L,int R,int v,int l,int r,int rt){
    if(L<=l&&r<=R){add(rt,v);return;}
    int m=(l+r)>>1;
    if(L<=m)upd(L,R,v,l,m,rt<<1);
    if(R>m)upd(L,R,v,m+1,r,rt<<1|1);
}
void qry(int l,int r,int rt){
    int m=(l+r)>>1,x,y,bg=tp;
    for(int i=head[rt];i;i=e[i].nxt){
        x=gf(X[e[i].to]),y=gf(Y[e[i].to]);
        if(x^y){
            if(siz[x]+siz[y]==n){fur(j,l,r)out("Connected\n");goto ski;}
            if(siz[x]>siz[y])SWAP(x,y);
            siz[f[st[++tp]=x]=y]+=siz[x];
        }
    }
    if(l==r)out("Disconnected\n");
    else qry(l,m,rt<<1),qry(m+1,r,rt<<1|1);
    ski:;
    for(;tp^bg;--tp)
        siz[f[x=st[tp]]]-=siz[x],f[x]=0;
}
int main(){
    in(n,k);
    fur(i,1,k)in(X[i],Y[i]);
    int c,x,la;
    list<int>::iterator it;
    in(q);
    fur(i,1,q)
        for(in(c);c;--c)
            in(x),T[x].push_back(i);
    fur(i,1,k){
        T[i].sort(),T[i].push_back(q+1);
        for(it=T[i].begin(),la=1;it!=T[i].end();la=*it+1,++it)
            if(la!=*it)upd(la,*it-1,i,1,q,1);
    }
    fur(i,1,n)siz[i]=1;
    qry(1,q,1);
    flush();
}
LG 5227 [AHOI2013]连通图
comment评论
Search
search