procedure2012
It's not worth it to know you're not worth it!

[关键字]:并查集

[题目大意]:有n个点m条边,要炸毁k个点,问按炸毁顺序炸毁每个点后有几个联通块。

//==============================================================================================

[分析]:正着做每次还要删边求联通块复杂度很高。而倒着做先吧所有点都干掉,然后并查集求出联通块个数,每次回复一个点然后合并联通块并更新当前答案,然后倒着输出答案。

[代码]:

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

using namespace std;



const int MAXN=410000;



struct node

{

int y,next;

}e[MAXN];

struct rec

{

int x,y;

}c[MAXN];

int first[MAXN];

int n,m,k,tot,ans;

int a[MAXN],f[MAXN],d[MAXN];

bool b[MAXN];



void Add(int x,int y)

{

++tot;

e[tot].y=y;

e[tot].next=first[x];

first[x]=tot;

}



int Find(int k)

{

if (f[k]==k) return k;

f[k]=Find(f[k]);

return f[k];

}



void Union(int x,int y,int &ans)

{

int xx=Find(x),yy=Find(y);

if (xx!=yy) f[xx]=yy,--ans;

}



void Init()

{

scanf("%d%d",&n,&m);

for (int i=0;i<n;++i) f[i]=i;

for (int i=1;i<=m;++i)

{

scanf("%d%d",&c[i].x,&c[i].y);

Add(c[i].x,c[i].y);

Add(c[i].y,c[i].x);

}

//for (int i=1;i<=m;++i) printf("%d %d\n",c[i].x,c[i].y);

scanf("%d",&k);

memset(b,0,sizeof(b));

for (int i=1;i<=k;++i)

{

scanf("%d",&a[i]);

b[a[i]]=1;

}

//for (int i=0;i<n;++i) printf("%d %d\n",i,b[i]);

}



void Solve()

{

for (int i=1;i<=m;++i)

if (!b[c[i].x] && !b[c[i].y]) Union(c[i].x,c[i].y,ans);

ans=0;

for (int i=0;i<n;++i)

if (f[i]==i && !b[i]) ++ans;

d[k+1]=ans;

for (int i=k;i>=1;--i)

{

b[a[i]]=0,++ans;

for (int j=first[a[i]];j;j=e[j].next)

if (!b[e[j].y]) Union(a[i],e[j].y,ans);

d[i]=ans;

}

for (int i=1;i<=k+1;++i) printf("%d\n",d[i]);

}



int main()

{

freopen("in.txt","r",stdin);

freopen("out.txt","w",stdout);

Init();

Solve();

return 0;

}



posted on 2012-03-12 07:40  procedure2012  阅读(167)  评论(0编辑  收藏  举报