https://www.luogu.org/problem/P1197

这道题算是关闭农场的加强版吧,数据有点大,矩阵存不下;

也是记录删点操作,从后往前加边;

先将每个点都算成一个连通块,然后每连一条边连通块数就减一;

加一个点时不要忘记连通块数+1,然后合并;

还有数组要开大;

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int pre[maxn*],last[maxn],other[maxn*],from[maxn*],l;
int n,m,k;
int out_block[maxn*],delete_order[maxn*];
int num_block[maxn*],tot_block;
int father[maxn*];
void add(int x,int y)
{
l++;
from[l]=x;
pre[l]=last[x];
last[x]=l;
other[l]=y;
} int getfather(int x)
{
if(father[x]==x) return x;
father[x]=getfather(father[x]);
return father[x];
} void merge(int x,int y)
{
int fx=getfather(x);
int fy=getfather(y);
father[fx]=fy;
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);add(y,x);
}
scanf("%d",&k);
for(int i=;i<=k;i++)
{
scanf("%d",&delete_order[i]);
out_block[delete_order[i]]=;
}
tot_block=n-k;
for(int i=;i<=n;i++) father[i]=i;
for(int i=;i<=m*;i++)
{
if(!out_block[from[i]]&&!out_block[other[i]]&&getfather(from[i])!=getfather(other[i]))
{
merge(from[i],other[i]);
tot_block--;
}
}
num_block[k+]=tot_block;
for(int i=k;i>=;i--)
{
out_block[delete_order[i]]=;
tot_block++;
for(int p=last[delete_order[i]];p;p=pre[p])
{
int v=other[p];
if(!out_block[v]&&getfather(delete_order[i])!=getfather(v))
{
tot_block--;
merge(delete_order[i],v);
}
}
num_block[i]=tot_block;
}
for(int i=;i<=k+;i++)
{
printf("%d\n",num_block[i]);
}
return ;
}

P1197 [JSOI2008]星球大战——链式前向星+并查集的更多相关文章

  1. 链式前向星+SPFA

    今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...

  2. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  3. hdu2647 逆拓扑,链式前向星。

    pid=2647">原文地址 题目分析 题意 老板发工资,可是要保证发的工资数满足每一个人的期望,比方A期望工资大于B,仅仅需比B多1元钱就可以.老板发的最低工资为888元.输出老板最 ...

  4. 图的存储结构:邻接矩阵(邻接表)&链式前向星

    [概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...

  5. 【模板】链式前向星+spfa

    洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...

  6. zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)

    2131: Can Win Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 431  Solved: 50 SubmitStatusWeb Board ...

  7. HDU1532 Drainage Ditches SAP+链式前向星

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂

    2130: hipercijevi Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 595  Solved: 112 SubmitStatusWeb B ...

  9. UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

    最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...

随机推荐

  1. sqlserver 统计每分钟内的数量

    1.统计每分钟内 url 的访问数量 SELECT SUBSTRING(CONVERT(varchar(100), date, 20), 0,17) as dateTime,COUNT(url) as ...

  2. Codeforces-975C - Valhalla Siege 前缀和 思维

    C. Valhalla Siege time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. SVN_05用戶管控

    安全性设置 [1]在左侧的User上点击右键 输入上面的信息,点击OK,我们就创建一个用户了. 说明:注意到了下面图中的Groups,是的,也可以先创建组,把用户添加到各个组中,然后对组进行授权,操作 ...

  4. Linux添加vsftp账户和设置目录权限

    改变store下面的所有.php文件属主为ftpd[root@www ~]# chgrp ftpd /store/*.php[root@www ~]# chown ftpd /store/*.php ...

  5. 今天还是python游戏

    话不多说,上源码: import random, pygame, sys from pygame.locals import * FPS = 30 # frames per second, the g ...

  6. JAVA 插入注解处理器

    JDK1.5后,Java语言提供了对注解(Annotation)的支持 JDK1.6中提供一组插件式注解处理器的标准API,可以实现API自定义注解处理器,干涉编译器的行为. 在这里,注解处理器可以看 ...

  7. switch语句中 参数的类型

    switch可作用于char byte short int switch可作用于char byte short int对应的包装类 switch不可作用于long double float boole ...

  8. Nginx跨域访问场景配置和防盗链

    跨域访问控制 跨域访问 为什么浏览器禁止跨域访问 不安全,容易出现CSRF攻击! 如果黑客控制的网站B在响应头里添加了让客户端去访问网站A的恶意信息,就会出现CSRF攻击 Nginx如何配置跨域访问 ...

  9. Android驱动之设备树简介

    目录 一.    设备树简介    2 1.    问题一:为什么需要设备树?    2 ①名词解释:    2 ②DT详细介绍:    2 ③DTS是DT的源文件,描述Device Tree中的设备 ...

  10. vue框架之脚手架(vue-cli)的使用

    前期准备 1.在使用之前需要安装node.js,https://nodejs.org/dist/latest-v8.x/ 2.下载之后在cmd中测试 node -v npm -v 如图上即可 3.下载 ...