n个结点,m条边构成一个连通图, 删除t条边,输出没删除一条边, 图会被分成几块
反向并查集,同样不难~
#include<bits/stdc++.h>
using namespace std;
const int maxn=100005;
int n,m;
int son[maxn][2],d[maxn],fa[maxn],a[maxn],sum[maxn];
inline int read()
{
int s=0,f=1;
char c=getchar();
while (c<'0'||c>'9')
{
if (c=='-')
{
f=-1;
}
c=getchar();
}
while (c>='0'&&c<='9')
{
s=s*10+c-48;
c=getchar();
}
return s*f;
}
int find(int x)
{
if (fa[x]==x)
{
return x;
}
return fa[x]=find(fa[x]);
}
int uni(int x,int y)
{
int fx=find(x),fy=find(y);
if (fx>fy)
{
fa[fx]=fy;
return 1;
}
else if (fx<fy)
{
fa[fy]=fx;
return 1;
}
return 0;
}
int main()
{
n=read(),m=read();
int x,y;
for(int i=1;i<=m;i++)
{
son[i][0]=read(),son[i][1]=read();
}
int num;
memset(a,0,sizeof(a));
for (int i=1;i<=n;i++)
{
fa[i]=i;
}
num=read();
for (int i=1;i<=num;i++)
{
d[i]=read();
a[d[i]]=1;
}
for (int i=1;i<=m;i++)
{
if (a[i]!=1)
{
uni(son[i][0],son[i][1]);
}
}
memset(sum,0,sizeof(sum));
for (int i=1;i<=n;i++)
{
if (fa[i]==i)
{
sum[num]++;
}
}
for (int i=num;i>=1;i--)
{
sum[i-1]=sum[i]-uni(son[d[i]][0],son[d[i]][1]);
}
for (int i=1;i<=num;i++)
{
cout<<sum[i]<<" ";
}
cout<<endl;
return 0;
}