越羽 @ 2019-05-25 23:06:30
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXX = 400005 ;
int head[400005],tot=0;
struct Node{
int from;
int to;
int next;
}bian[MAXX];
bool flag[MAXX];
int f[MAXX],ans[MAXX],h[MAXX];
int n,m,k,lian=0;
inline int find(int x){
if(f[x]==x)return x;
else return f[x]=find(f[x]);
}
inline void add(int x,int y){
bian[tot].to=y;
bian[tot].from=x;
bian[tot].next=head[x];
head[x]=tot;
tot++;
}
inline void adda(int x,int y){
f[find(x)]=find(y);
}
int main(){
ios::sync_with_stdio(0);
cin>>n>>m;
memset(head,0,sizeof(head));
for(register int i=0;i<=n;i++)f[i]=i;
for(register int i=0;i<m;i++){
int x,y;
cin>>x>>y;
add(x,y);
add(y,x);
}
cin>>k;
for(register int i=1;i<=k;i++){
cin>>h[i];
flag[h[i]]=1;
}
lian=n-k;
for(register int i=0;i<2*m;i++){
int x=bian[i].from;
int y=bian[i].to;
if(flag[x]==0&&flag[y]==0){
if(find(x)!=find(y)){
lian--;
adda(x,y);
}
}
}
ans[k+1]=lian;
for(register int i=k;i>0;i--){
lian++;
int x=h[i];
flag[h[i]]=0;
for(register int j=head[x];j!=0;j=bian[j].next){
if(flag[bian[j].to]==0&&f[x]!=f[find(bian[j].to)]){
lian--;
adda(bian[j].to,x);
}
}
ans[i]=lian;
}
for(register int i=1;i<=k+1;i++)cout<<ans[i]<<endl;
return 0;
}
by w_57 @ 2019-07-07 14:28:40
感觉和我错一个地方了,adda(bian[j].to,x)的x要每次找,因为并查集合并的时候父亲在变