靠一开始并查集没写路径压缩
后来qyf才找出来
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define MAX 500000+900
#define pb push_back
using namespace std;
int n,m,k,ask[MAX],if_ask[MAX],father[MAX];
int answer[MAX],can[MAX]={0};
vector<int>aim[MAX];
struct wbysr
{
int from,to;
}a[200000+9];
int get_father(int x)
{
if(father[x]==x)
return x;
father[x]=get_father(father[x]);
return father[x];
}
void Union(int x,int y)
{
father[get_father(x)]=get_father(y);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int a1,a2;
scanf("%d%d",&a1,&a2);
aim[a1+1].pb(a2+1);
aim[a2+1].pb(a1+1);
a[i].from=a1+1;
a[i].to=a2+1;
}
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
int a1;
scanf("%d",&a1);
a1++;
if(if_ask[a1])
a1=0;
ask[i]=a1;
if_ask[a1]=1;
can[a1]=1;
}
/* printf("----------------------------------------\n");
for(int i=1;i<=n;i++)
for(int j=0;j<aim[i].size();j++)
printf("%d %d\n",i,aim[i][j]);
for(int i=1;i<=k;i++)
printf("~~~ %d\n",ask[i]);
printf("------------------------------------------\n");*/
for(int i=1;i<=n;i++)
father[i]=i;
/*for(int i=1;i<=n;i++)
if(!if_ask[i])
for(int j=0;j<aim[i].size();j++)
if(!if_ask[aim[i][j]]&&get_father(i)!=get_father(aim[i][j]))
Union(i,aim[i][j]);*/
for(int i=1;i<=m;i++)
if(!if_ask[a[i].from]&&!if_ask[a[i].to])
{
int fa=get_father(a[i].from),fb=get_father(a[i].to);
if(fa!=fb)
father[fa]=fb;
}
int now=0;
for(int i=1;i<=n;i++)
if(!if_ask[i]&&father[i]==i)
now++;
/* for(int i=1;i<=n;i++)
if(!if_ask[i])
for(int j=0;j<aim[i].size();j++)
if(!if_ask[aim[i][j]])
printf("%d->%d\n",i,aim[i][j]);*/
answer[k]=now;
for(int i=k;i>=1;i--)
{
if(can[ask[i]])
now++;
can[ask[i]]=0;
// printf("%d %d %d!!!!!\n",i,ask[i],now);
for(int j=0;j<aim[ask[i]].size();j++)
if(!can[aim[ask[i]][j]])
{
int fa=-1;
// printf("%d -%d-%d-%d -%d\n",aim[ask[i]][j],now,can[aim[ask[i]][j]],get_father(ask[i]),get_father(aim[ask[i]][j]));
if(get_father(ask[i])!=get_father(aim[ask[i]][j]))
{
now--;
Union(ask[i],aim[ask[i]][j]);
}
}
answer[i-1]=now;
// for(int i=1;i<=n;i++)
// printf("%d %d\n",i,get_father(i));
// printf("answer %d\n",now);
}
for(int i=0;i<=k;i++)
printf("%d\n",answer[i]);
return 0;
}