• BZOJ 1015 JSOI2008 星球大战 starwar 并检查集合


    标题效果:给定一个无向图。联通谋求块的数目,以及k一个点的破坏后每次;联通,块的数目

    侧面和摧毁的地步全记录,我们可以做相反的。

    需要注意的是该点不能算作破坏联通块

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define M 400400
    using namespace std;
    struct abcd{
    	int to,next;
    }table[M];
    int head[M],tot;
    int n,m,q;
    int fa[M],stack[M],destroy[M],top,now;
    bool destroyed[M];
    int Find(int x)
    {
    	if(!fa[x]||fa[x]==x)
    		return fa[x]=x;
    	return fa[x]=Find(fa[x]);
    }
    inline void Unite(int x,int y)
    {
    	int fx=Find(x);
    	int fy=Find(y);
    	if(fx==fy)
    		return ;
    	--now;
    	fa[fy]=fx;
    }
    inline void Add(int x,int y)
    {
    	table[++tot].to=y;
    	table[tot].next=head[x];
    	head[x]=tot;
    }
    int main()
    {
    	int i,j,x,y;
    	cin>>n>>m;
    	for(i=1;i<=m;i++)
    	{
    		scanf("%d%d",&x,&y);
    		++x;++y;
    		Add(x,y);
    		Add(y,x);
    	}
    	cin>>q;
    	for(i=1;i<=q;i++)
    	{
    		scanf("%d",&destroy[i]);
    		++destroy[i];
    		destroyed[destroy[i]]=1;
    	}
    	now=n-q;
    	for(j=1;j<=n;j++)
    		if(!destroyed[j])
    			for(i=head[j];i;i=table[i].next)
    				if(!destroyed[table[i].to])
    					Unite(j,table[i].to);
    	stack[++top]=now;
    	for(j=q;j;j--)
    	{
    		x=destroy[j];
    		destroyed[x]=0;
    		++now;
    		for(i=head[x];i;i=table[i].next)
    			if(!destroyed[table[i].to])
    				Unite(x,table[i].to);
    		stack[++top]=now;
    	}
    	while(top)
    		printf("%d
    ",stack[top--]);
    }
    


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Java生产环境线上栈故障排查问题(COPY)
    Java集合HashMap,List底层
    图算法--染色法判定二图
    图算法--kruskal
    图算法--最小生成树prim
    图算法--判负环
    图算法--floyd
    图算法--spfa
    图算法--bellman-ford (nm)
    图算法--堆优化版dijkstra
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4854754.html
Copyright © 2020-2023  润新知