https://www.luogu.org/problemnew/show/P1197

题解:倒向并查集

C++版本一

#include<iostream>
#include<cstdio>
#define f(i,a,b) for(register int i=a;i<=b;i++)
#define fd(i,a,b) for(register int i=a;i>=b;i--)
using namespace std;
int k,n,m,head[400002],tot,broken[400002],ans[400003];
int father[400003];
struct Node
{
    int next,node,from;
}h[400002];
inline void Add_Node(int u,int v)
{
    h[++tot].from=u;
    h[tot].next=head[u];
    head[u]=tot;
    h[tot].node=v;
}
bool Broken[400001];
inline int Get_father(int x)
{
    if(father[x]==x)    return x;
    return father[x]=Get_father(father[x]);
    //你爸爸的爸爸就是你的爸爸——反查理马特——并查集 
}
inline void hb(int u,int v)
{
    u=Get_father(u),v=Get_father(v);
    if(u!=v)    father[v]=u;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    f(i,0,n)
        father[i]=i,head[i]=-1;//并查集初始化 
    f(i,1,m)
    {
        int x,y;
        cin>>x>>y;
        Add_Node(x,y);//储存图 
        Add_Node(y,x);//由于无向图存两遍 
    }
    cin>>k;
    f(i,1,k)
    {
        cin>>broken[i];
        Broken[broken[i]]=1;//标记砸坏了 
    }
    int total=n-k;//初始化为所有点都是单独存在的 
    f(i,1,2*m)//有2*m个边 
        if(!Broken[h[i].from] && !Broken[h[i].node] && Get_father(h[i].from)!=Get_father(h[i].node))
{//要是起点和终点都没砸坏 而且他们并没有联通

            total--;//连一条边 减一个联通体 
            hb(h[i].from,h[i].node);
        }
    ans[k+1]=total;//当前就是最后一次破坏后的个数 
    fd(i,k,1)
    {
        //total=0 //这里不需要初始化 需要从上一次的废墟上修建 
        total++;//修复一个点 联通体+1 
        Broken[broken[i]]=0;//修复 
        for(int j=head[broken[i]];j!=-1;j=h[j].next)//枚举每一个子点 
        {
            if(!Broken[h[j].node] && Get_father(broken[i])!=Get_father(h[j].node))
            {
                total--;//连一边减一个联通块 
                hb(broken[i],h[j].node);//合并这两个点 
            }
        }
        ans[i]=total;
    }
    f(i,1,k+1)    cout<<ans[i]<<endl;
    return 0;
}

C++版本二

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=400000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans[N],cnt,flag,temp,sum;
vector<int>G[N];
int broken[N];
bool Broken[N];
int pre[N];
char str;
struct node{};
int find(int x){return (pre[x]==x)?x:pre[x]=find(pre[x]);}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        pre[i]=i;
    }
    int x,y;
    for(int i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    scanf("%d",&k);
    cnt=n-k;
    for(int i=1;i<=k;i++){
        scanf("%d",&broken[i]);
        Broken[broken[i]]=1;
    }

    for(int i=0;i<n;i++){
        for(int j=0,sz=G[i].size();j<sz;j++){
            int u=i;
            int v=G[i][j];
            int tx=find(u);//cout<<u<<" ";
            int ty=find(v);//cout<<v<<endl;
            if(!Broken[u]&&!Broken[v]&&tx!=ty){
                cnt--;
                pre[tx]=ty;
            }
        }
    }
    ans[k+1]=cnt;

    for(int i=k;i>=1;i--){
        cnt++;
        Broken[broken[i]]=0;
        for(int j=0,sz=G[broken[i]].size();j<sz;j++){
            int u=broken[i];
            int v=G[broken[i]][j];//cout<<j<<" ";
            int tx=find(u);//cout<<u<<" ";
            int ty=find(v);//cout<<v<<endl;
            if(!Broken[u]&&!Broken[v]&&tx!=ty){
                cnt--;
                pre[tx]=ty;
            }
        }
        ans[i]=cnt;
    }
    for(int i=1;i<=k+1;i++){
        cout<<ans[i]<<endl;
    }
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}