并查集

正序处理时间复杂度为n^2,考虑逆序处理,这样,时间复杂度从n^2降为nlogn

附上代码:

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
#define N 400005
int fa[N<<1],n,m,K,head[N<<1],cnt,q[N],ans[N];
struct node
{
int to,next;
}e[N<<1];
void add(int x,int y)
{
e[cnt].to=y;
e[cnt].next=head[x];
head[x]=cnt++;
return ;
}
int find(int x)
{
int p=x,t;
while(fa[p]!=p)p=fa[p];
while(x!=p)t=fa[x],fa[x]=p,x=t;
return p;
}
bool vis[N];
int main()
{
for(int i=1;i<N;i++)fa[i]=i;
memset(head,-1,sizeof(head));
memset(vis,1,sizeof(vis));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x++,y++;
add(x,y);
add(y,x);
}
scanf("%d",&K);
ans[K]=n-K;
for(int i=1;i<=K;i++)
{
scanf("%d",&q[i]);
q[i]++;
vis[q[i]]=0;
}
for(int i=1;i<=n;i++)
{
if(!vis[i])continue;
for(int j=head[i];j!=-1;j=e[j].next)
{
int to1=e[j].to;
if(!vis[to1])continue;
int fx=find(i),fy=find(to1);
if(fx!=fy)
{
fa[fx]=fy;
ans[K]--;
}
}
}
vis[q[K]]=1;
for(int i=K-1;i>=0;i--)
{
ans[i]=ans[i+1]+1;
for(int j=head[q[i+1]];j!=-1;j=e[j].next)
{
int to1=e[j].to;
if(!vis[to1])continue;
int fx=find(q[i+1]),fy=find(to1);
if(fx!=fy)
{
fa[fx]=fy;
ans[i]--;
}
}
vis[q[i]]=1;
}
for(int i=0;i<=K;i++)
{
printf("%d\n",ans[i]);
}
return 0;
}

  

[JSOI2008]星球大战starwar BZOJ1015的更多相关文章

  1. BZOJ1015[JSOI2008]星球大战starwar[并查集]

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 5253  Solved: 2395[Submit ...

  2. bzoj1015:1015: [JSOI2008]星球大战starwar

    应该是全部读入之后再添加边用并查集就可以了. yyl用空间换时间.u[]v[]等将边预存起来. #include<cstdio> #include<cstring> #incl ...

  3. BZOJ1015 [JSOI2008]星球大战starwar(并查集)

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 3895  Solved: 1750[Submit ...

  4. [Bzoj1015][JSOI2008]星球大战starwar(并查集)(离线处理)

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 6849  Solved: 3204[Submit ...

  5. 1015: [JSOI2008]星球大战starwar

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MB Description 很久以前,在一个遥远的星系,一个黑暗的帝国 ...

  6. [1015][JSOI2008]星球大战starwar(并查集)

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 2124  Solved: 909[Submit] ...

  7. BZOJ 1015: [JSOI2008]星球大战starwar 并查集

    1015: [JSOI2008]星球大战starwar Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝 ...

  8. 【BZOJ】1015: [JSOI2008]星球大战starwar

    1015: [JSOI2008]星球大战starwar 题意:一个点数为N(1<= 40w),边数为M(1<=20w)的图,总共删除k个节点,问开始以及每次删除一个节点之后图的连通块数? ...

  9. BZOJ 1015 [JSOI2008]星球大战starwar

    1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 3551  Solved: 1581[Submit ...

随机推荐

  1. Java Selenium 定位元素 实现的一个注册功能

    import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; impor ...

  2. 中国的UED们

    UED网址导航:http://www.ux265.com/ 天猫UED:http://ued.tmall.com/ 一淘UED:http://ux.etao.com/ 淘宝UED:http://ued ...

  3. DELETE_FAILED_INTERNAL_ERROR Error while Installing APK

    真是Android2.3的特殊版本问题,问题原因是android2.3的instant run的测试版安装方式有所特别,解决办法有2: 1.手动adb install 安装包 2.把Instant r ...

  4. Python函数式实现单例特性

    传统的单例一般是基于类的特性实现,Python模块是天生的单例,下面来个简单的借助模块和函数实现单例特性: gdb = None def get_gdb(): global gdb if gdb is ...

  5. TabLayout:另一种Tab的实现方式

    http://blog.csdn.NET/aigestudio/article/details/47155769 在5.0以前我们想要实现像网易新闻客户端那样的的Tab可以有很多种选择: 比如古老的T ...

  6. PermutationTwo

    Description: Given a collection of numbers that might contain duplicates, return all possible unique ...

  7. 让 Homebrew 走代理更新 + brew 管理 node 版本

    0.前言 环境:MacOS 背景:整理下今天所做的配置. 1. 让 Homebrew 走代理更新 brew update 就卡住了,即使开了 shadowsocks 也不行.因为 shadowsock ...

  8. spark SQL随笔

    sparkSQL 1.主要的数据结构 DataFreames 2.开始使用:SQLContext 创建步骤: Val  sc:sparkContext Val  sqlContext=new org. ...

  9. HTML DOM innerHTML 属性及实现图片连续播放

    定义和用法 innerHTML 属性设置或返回表格行的开始和结束标签之间的 HTML. 语法 tablerowObject.innerHTML=HTML 实例 下面的例子返回了表格行的 inner H ...

  10. 网络-tcp

    1.TCP:面向连接可靠的传输协议,全拼:Transmission Control Protocol   2.UDP:用户数据报协议 全拼:User Datagram protocol 不是面向连接的 ...