40pts,玄关

P1197 [JSOI2008] 星球大战

DgNeHzL77777 @ 2024-09-28 18:58:18

#include<iostream>

#include<cstring>
#define N 400005
#define M 400005
using namespace std;
int n,m,k;
int head[N],from[M],to[M],nxt[M],idx=-1;
bool br[N];
int ks[N],ans[N],nw;
void add(int x,int y){
    ++idx;
    from[idx]=x;
    to[idx]=y;
    nxt[idx]=head[x];
    head[x]=idx;
}
int fa[N];
int find(int x){
    if(x!=fa[x]) fa[x]=find(fa[x]);
    return fa[x];
}
signed main(){
    cin>>n>>m;
    memset(head,-1,sizeof head);
    for(int i=0;i<n;++i){
        fa[i]=i;
    }
    while(m--){
        int x,y;cin>>x>>y;
        add(x,y);add(y,x);
    }
    cin>>k;
    nw=n-k;
    for(int i=1;i<=k;++i){
        int x;cin>>x;
        br[x]=1,ks[i]=x;
    }
    for(int i=0;i<m*2;++i){
        if(br[from[i]]==0&&br[to[i]]==0){
            if(find(from[i])!=find(to[i])){
                fa[find(from[i])]=fa[find(to[i])];
                --nw;
            }
        }
    }
    ans[k+1]=nw;
    for(int t=k;t>=1;--t){
        int u=ks[t];
        br[u]=0;
        ++nw;
        for(int i=head[u];i!=-1;i=nxt[i]){
            if(br[to[i]]==0&&fa[find(u)]!=fa[find(to[i])]){
                fa[find(to[i])]=fa[find(u)];
                --nw;
            }
        }
        ans[t]=nw;
    }
    for(int i=1;i<=k+1;++i){
        cout<<ans[i]<<"\n";
    }
    return 0;
}

by DgNeHzL7777 @ 2024-09-28 19:09:13

不能while(m--){

}

此帖节


|