#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,p;
int en;
int head[400005],bo[400005],h[400005],fa[400005],an[400005];
struct node
{
int from,mid,to;
}a[400005];
void add(int x,int y)
{
a[en].from=x;
a[en].mid=head[x];
a[en].to=y;
head[x]=en;
en++;
}//链式向前星
int find(int x)
{
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
void hb(int x,int y)//合并
{
int a=find(x);
int b=find(y);
fa[a]=b;
}
signed main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
fa[i]=i;
head[i]=-1;
}
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
add(x,y);
add(y,x);
}
int k;
cin>>k;
for(int i=0;i<k;i++)
{
cin>>h[i];
bo[h[i]]=1;
}
int t=n-k;
for(int i=0;i<2*m;i++)
{
if(bo[a[i].from]==0&&bo[a[i].to]==0)
{
if(find(a[i].to)!=find(a[i].from))
{
t--;
hb(a[i].to,a[i].from);
}
}
}
an[k]=t;
int u;
for(int i=k-1;i>=0;i--)
{
u=h[i];
t++;
bo[u]=0;
for(int j=head[u];j!=-1;j=a[j].mid)
{
if(bo[a[j].to]==0&&fa[find(u)]!=fa[find(a[j].to)])
{
t--;
hb(u,a[j].to);//顺序可换
}
}
an[i]=t;
}
for(int i=0;i<=k;i++)
{
cout<<an[i]<<endl;
}
}
/*泛舟沧海,立马昆仑*/
/*若你为了错过太阳而哭泣,那么你也将错过群星了*/
/*红了樱桃,绿了芭蕉*/
/*泛舟沧海,立马昆仑*/
/*若你为了错过太阳而哭泣,那么你也将错过群星了*/
/*红了樱桃,绿了芭蕉*/
/*
3 3
0 3
1 2
0 2
*/
自写版,关键在于t用来统计联通块数量
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,p;
int fa[400111];
int bo[400111];
int find(int x)
{
if(fa[x]==x) return x;
else
return fa[x]=find(fa[x]);
}
int hb(int a,int b)
{
int x=find(a);
int y=find(b);
if(x!=y)
{
fa[y]=x;
return 1;
}
else return 0;
}
signed main()
{
cin>>n>>m;
for(int i=0; i<=n-1; i++)
fa[i]=i;
vector<int> g[n+1];
for(int i=0; i<m; i++)
{
int x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
int k;
cin>>k;
int t=n-k;
vector<int> a(k);
vector<int> an(k+1);
memset(bo,0,sizeof(bo));
for(int i=0; i<=k-1; i++)
{
cin>>a[i];
bo[a[i]]=1;
}
for(int j=0; j<=n-1; j++)
{
int l=g[j].size();
for(int w=0; w<l; w++)
{
if(bo[j]==0&&bo[g[j][w]]==0)
{
if(hb(j,g[j][w]))
t--;
}
}
}
an[k]=t;
for(int i=k-1; i>=0; i--)
{
bo[a[i]]=0;
t++;
int l=g[a[i]].size();
for(int w=0; w<l; w++)
{
if(bo[g[a[i]][w]]==0)
{
if(hb(a[i],g[a[i]][w]))
t--;
}
}
an[i]=t;
}
for(int i=0; i<=k; i++)
cout<<an[i]<<endl;
}
/*泛舟沧海,立马昆仑*/
/*若你为了错过太阳而哭泣,那么你也将错过群星了*/
/*红了樱桃,绿了芭蕉*/
/*泛舟沧海,立马昆仑*/
/*若你为了错过太阳而哭泣,那么你也将错过群星了*/
/*红了樱桃,绿了芭蕉*/
/*
3 3
0 3
1 2
0 2
*/
借鉴洛谷大佬:花开依然爱Qu
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXI=4e5+4;
int f[MAXI],head[MAXI],h[MAXI],ans[MAXI],En=0;//f为并查集,h为打击点存储的数组,ans为每次打击后的答案
bool e[MAXI]; //e来判断是否被打击掉
int find(int x)
{
if(x!=f[x]) f[x]=find(f[x]); //并查集基本函数
return f[x];
}
struct edge
{
int from;
int to; //定义一个结构体来存储邻接表
int next;
}a[MAXI];
void insert(int u,int v)
{ //邻接表存储数据
a[En].from=u;
a[En].next=head[u];
a[En].to=v;
head[u]=En;
En++;
}
int main()
{
int n,m,k,x,y,tot,i,u;
cin>>n>>m;
for(i=0;i<n;++i)
{
f[i]=i;
head[i]=-1;
}
for(i=0;i<m;++i)
{
cin>>x>>y;
insert(x,y);insert(y,x); //双向存储数据
}
cin>>k;
tot=n-k; //打击k次后所剩下的点
for(i=1;i<=k;i++)
{
cin>>x;
e[x]=true; //被打击掉后就true,并把打击的点存储到h中
h[i]=x;
}
for(i=0;i<2*m;i++)
{
if(e[a[i].from]==false&&e[a[i].to]==false) //如果都没有被打击
{
if(find(a[i].from)!=find(a[i].to)) //且之前没有连通
{
tot--; //合并这两个点并在总数减去一个
f[find(a[i].from)]=f[find(a[i].to)];
}
}
}
ans[k+1]=tot; //这时为打击k次之后所剩下的连通块
for(int t=k;t>=1;t--) //从后往前“修复”
{
u=h[t];
tot++; //因为“修复”这个点所以多了一个点,现在总数加 1
e[u]=false; //false表示这个点没有被打击
for(i=head[u];i!=-1;i=a[i].next) //邻接表遍历它所连着的点
{
if(e[a[i].to]==false&&f[find(u)]!=f[find(a[i].to)]) //如果被连通的点没有被打击并且之前没有连通
{
tot--; //合并
f[find(a[i].to)]=f[find(u)]; //注意尽量不要到过来赋值,这样会不断改变father
}
}
ans[t]=tot; //每“修复”一个点后的有的连通块
}
for(i=1;i<=k+1;++i) cout<<ans[i]<<endl;
return 0;
}