Star Wars JSOI2008] [[disjoint-set]

[JSOI2008] Star Wars

We decided to do an investigation and catchment issues ....

Being difficult now to attack the anti planet are then read into the record and then damaged even are able to connect to the

Then the routine is done backwards QAQ first post codes dying did not seriously read the question ... is the number to start from zero in

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5,M=2e5+5,inf=0x3f3f3f3f,P=19650827;
int n,m,k,cnt=0,ans[N],a[N];
bool broken[N];
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

int head[N],tot=0;
struct edge{int v,nxt;}e[M<<1];
void add(int u,int v){
    e[++tot]=(edge){v,head[u]},head[u]=tot;
}

int f[N];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
void build(int x,int y){
    if(broken[y]) return;
    if(find(x)!=find(y)) --cnt,f[f[x]]=f[y];
}
int main(){
    freopen("in2.txt","r",stdin);
    //freopen("xor.out","w",stdout);
    rd(n),rd(m);
    for(int i=1,u,v;i<=m;++i) rd(u),rd(v),add(u,v),add(v,u);
    rd(k);
    for(int i=k;i;--i) rd(a[i]),broken[a[i]]=1;
    for(int i=0;i<n;++i) f[i]=i;
    for(int i=0;i<n;++i)
    if(!broken[i]){
        ++cnt;
        for(int j=head[i];j;j=e[j].nxt) build(i,e[j].v);
    }
    ans[1]=cnt;
    for(int t=1,x;t<=k;++t){
        x=a[t],broken[x]=0,++cnt;
        for(int i=head[x];i;i=e[i].nxt) build(x,e[i].v);
        ans[t+1]=cnt;
    }
    for(int i=k+1;i;--i) printf("%d\n",ans[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/lxyyyy/p/11374012.html