BZOJ 1015: [JSOI2008]星球大战starwar(并查集求连通块+离线处理)
http://www.lydsy.com/JudgeOnline/problem.php?id=1015
题意:

思路:
好题啊!!!
这道题目需要离线处理,先把所有要删的点给保存下来,然后逆序加点,这样就把原来的删点变为了加点,加点的话计算连通块就方便的多,具体参见代码。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m;
int k;
int tot;
int cnt;
int q[*maxn];
int p[*maxn];
int vis[*maxn];
int del[*maxn];
int ans[*maxn];
int head[*maxn]; struct node
{
int v,next;
}e[*maxn]; void addEdge(int u,int v)
{
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
} int Find(int x)
{
return x==p[x]?x:p[x]=Find(p[x]);
} void update(int u)
{
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
if(vis[v]) //如果v之前已经访问过了并且u和v不在同一连通块中,那么合并并且减少一个连通块
{
int x=Find(u);
int y=Find(v);
if(x!=y) {p[x]=y;cnt--;}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
tot=;
memset(head,-,sizeof(head));
memset(del,,sizeof(del));
memset(vis,,sizeof(vis));
for(int i=;i<n;i++) p[i]=i;
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v);
addEdge(v,u);
} scanf("%d",&k);
for(int i=;i<=k;i++) {scanf("%d",&q[i]);del[q[i]]=;} cnt=;
for(int i=;i<n;i++)
{
if(!del[i])
{
cnt++; //先把它单独作为一个连通块
update(i);
vis[i]=;
}
} ans[k+]=cnt;
for(int i=k;i>=;i--) //加点
{
cnt++;
update(q[i]);
vis[q[i]]=;
ans[i]=cnt;
}
for(int i=;i<=k+;i++) printf("%d\n",ans[i]);
}
return ;
}
BZOJ 1015: [JSOI2008]星球大战starwar(并查集求连通块+离线处理)的更多相关文章
- BZOJ 1015: [JSOI2008]星球大战starwar 并查集
1015: [JSOI2008]星球大战starwar Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝 ...
- P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)
P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- BZOJ1015[JSOI2008]星球大战starwar[并查集]
1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 5253 Solved: 2395[Submit ...
- BZOJ 1015 [JSOI2008]星球大战starwar
1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 3551 Solved: 1581[Submit ...
- bzoj 1015: [JSOI2008]星球大战starwar (逆向思维+并查集)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1015 思路: 题目是要我们对当前图拆掉k个点,问,每拆一个点后图中有多少个联通块,我们可以逆 ...
- BZOJ 1015: [JSOI2008]星球大战starwar【并查集】
题目可以表述成:给定一个无向图G,每次删除它的一个点和与点相关的边集,每次询问该操作后图G的连通度(连通分量的个数).和上一题一样都是考察逆向思维,虽然删除点的做法不会,但是每次加点后询问连通度却是并 ...
- 1015. [JSOI2008]星球大战【并查集】
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的 机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通 ...
- BZOJ 1015 JSOI2008 星球大战 starwar 并检查集合
标题效果:给定一个无向图.联通谋求块的数目,以及k一个点的破坏后每次:联通,块的数目 侧面和摧毁的地步全记录,我们可以做相反的. 需要注意的是该点不能算作破坏联通块 #include<cstdi ...
随机推荐
- CentOS工作内容(五)单一网卡配置多个IP
CentOS工作内容(五)单一网卡配置多个IP 用到的快捷键 tab 自动补齐(有不知道的吗) ctrl+a 移动到当前行的开头(a ahead) ctrl+e 移动到当前行的开头(e end) ct ...
- mysql备份工具innobackupex,xtrabackup-2.1的原理和安装
mysql备份工具innobackupex,xtrabackup-2.1的原理和安装 http://bbs.2cto.com/read.php?tid=310496 一.Xtrabackup介绍 1. ...
- [py]可迭代对象-求最值
for .. in ..方式遍历可迭代对象 而非下标 ## 判断是否可迭代 from collections import Iterable print(isinstance(123,Iterable ...
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- 浅谈远程登录时,ssh的加密原理
SSH:Secure Shell,是一种网络安全协议,主要用于登录远程计算机的加密过程. 登录方式主要有两种: 1.基于用户密码的登录方式: 加密原理: 当服务器知道用户请求登录时,服务器会把 ...
- app加密算法
php端 <?phpnamespace app\controllers;use yii\web\Controller;class TestController extends Controlle ...
- Java实现递归将嵌套Map里的字段名由驼峰转为下划线
摘要: 使用Java语言递归地将Map里的字段名由驼峰转下划线.通过此例可以学习如何递归地解析任意嵌套的List-Map容器结构. 难度:初级 概述 在进行多语言混合编程时,由于编程规范的不同, 有时 ...
- wamp下配置多域名和访问路径的方法
wamp下配置多域名和访问路径的方法 1.到安装目录下,打开配置httpd.confD:\wamp\bin\apache\Apache2.2.21\conf\httpd.conf也可以通过wamp图标 ...
- php array 根据value获取key,in_array()判断是否在数组内实例
php array 根据value获取key,in_array()判断是否在数组内实例 <?php header("Content-type: text/html; charset=u ...
- 在python3下使用OpenCV 显示图像
在Python3下用使用OpenCV比在C,C++里开发不止快捷一点点, 原型开发的时候蛮有用. 这里用的OpenCV 加载图片, 用的imshow画图 # -*- coding: utf-8 -*- ...