zoukankan      html  css  js  c++  java
  • 并查集【洛谷P1197】 [JSOI2008]星球大战

    P1197 [JSOI2008]星球大战

    题目描述

    很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系。

    某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球。这些星球通过特殊的以太隧道互相直接或间接地连接。

    但好景不长,很快帝国又重新造出了他的超级武器。凭借这超级武器的力量,帝国开始有计划地摧毁反抗军占领的星球。由于星球的不断被摧毁,两个星球之间的通讯通道也开始不可靠起来。

    现在,反抗军首领交给你一个任务:给出原来两个星球之间的以太隧道连通情况以及帝国打击的星球顺序,以尽量快的速度求出每一次打击之后反抗军占据的星球的连通块的个数。(如果两个星球可以通过现存的以太通道直接或间接地连通,则这两个星球在同一个连通块中)。

    倒序并查集,大概五天前还做不出来。

    菜鸡菜鸡。。。

    code:

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    const int wx=400017;
    
    inline int read(){
    	int sum=0,f=1; char ch=getchar();
    	while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    	while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
    	return sum*f;
    }
    
    int head[wx],fa[wx];
    int flag[wx],que[wx],ans[wx];
    int num,n,m,k,tmp;
    
    struct e{
    	int nxt,to;
    }edge[wx*2];
    
    void add(int from,int to){
    	edge[++num].nxt=head[from];
    	edge[num].to=to;
    	head[from]=num;
    }
    
    int find(int x){
    	if(x==fa[x])return x;
    	return fa[x]=find(fa[x]);
    }
    
    int main(){
    	n=read(); m=read();
    	for(int i=1;i<=m;i++){
    		int x,y;
    		x=read(); y=read(); x++; y++;
    		add(x,y); add(y,x);
    	}
    	k=read();
    	for(int i=1;i<=k;i++){
    		int x; x=read(); x++;
    		que[i]=x; flag[x]=1;
    	}
    	for(int i=1;i<=n;i++)fa[i]=i;
    	int tot=n-k;
    	for(int u=1;u<=n;u++){
    		if(flag[u])continue;
    		for(int i=head[u];i;i=edge[i].nxt){
    			int v=edge[i].to;
    			if(flag[v])continue;
    			int fx=find(u); int fy=find(v);
    			if(fx!=fy){
    				fa[fx]=fy; tot--;
    			}
    		}
    	}
    	ans[++tmp]=tot;
    	for(int i=k;i>=1;i--){
    		int now=que[i]; tot++;
    		for(int j=head[now];j;j=edge[j].nxt){
    			int v=edge[j].to;
    			if(flag[v])continue;
    			int fx=find(now); int fy=find(v);
    			if(fx==fy)continue;
    			fa[fx]=fy; tot--;
    		}
    		ans[++tmp]=tot;
    		flag[now]=0;
    	}
    	for(int i=tmp;i>=1;i--)printf("%d
    ",ans[i]);
    	return 0;
    }
  • 相关阅读:
    Linux NFS服务器的安装与配置
    mysql 批量更新的四种方法
    解决 RHEL 7/ CentOS 7/Fedora 出现Unit iptables.service failed to load
    linux 搭建svn
    MYSQL的慢查询两个方法
    Apache 配置虚拟主机三种方式
    MYSQL explain详解
    php操作memcache的使用【转】
    PHPExcel中open_basedir restriction in effect的解决方法
    微信浏览器禁止app下载链接的两种处理方法
  • 原文地址:https://www.cnblogs.com/wangxiaodai/p/9858433.html
Copyright © 2011-2022 走看看