萌新刚学OI,只有40分诶

P1197 [JSOI2008] 星球大战

LevenKoko @ 2019-03-17 20:32:49

RT,刚学OI的萌新连并查集都不会ne...

#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
inline int read(){
    int ans=0,f=1;char chr=getchar();
    while(!isdigit(chr)){if(chr=='-') f=-1;chr=getchar();}
    while(isdigit(chr)){ans=(ans<<3)+(ans<<1)+chr-48;chr=getchar();}
    return ans*f;
}
int n,m,fa[2000005],k,vis[2000005],p[2000005],ANS[2000005];
vector<int> v[2000005];
int find(int x) {
    if(fa[x]==x) return x;
    return fa[x]=find(fa[x]);
}
int main() {
    n=read();
    m=read();
    for(int i=1; i<=n+1; i++) fa[i]=i;
    for(int i=1; i<=m; i++) {
        int x=read(),y=read();
        ++x,++y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    k=read();
    for(int i=1; i<=k; i++) p[k-i+1]=read(),++p[k-i+1],vis[p[k-i+1]]=1;
    for(int i=1; i<=n; i++)if(!vis[i])
            for(int j=0; j<v[i].size(); j++) {
                if(vis[v[i][j]]) continue;
                int fx=find(i),fy=find(v[i][j]);
                if(fx!=fy) fa[fx]=fy,ANS[1]--;
            }
    for(int i=1; i<=n; i++) if(fa[i]==i) ANS[1]++;
    ANS[1]=n-k;
    for(int i=1,t; i<=k; i++) {
        t=ANS[i];
        vis[p[i]]=0;
        for(int j=0; j<v[p[i]].size(); j++) {
            if(vis[v[p[i]][j]]) continue;
            int fx=find(p[i]),fy=find(v[p[i]][j]);
            if(fx!=fy) fa[fx]=fy,--t;
        }
        ANS[i+1]=t+1;
    }
    reverse(ANS+1,ANS+k+2);
    for(int i=1; i<=k+1; i++)   cout<<ANS[i]<<endl;
    return 0;
}

|