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 ...
随机推荐
- Kubernetes 1.8火热出炉:稳定性、安全性与存储支持能力全面提升
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/78130225 周三,Kub ...
- 9 jmeter之检查点
jmeter有类似loadrunner检查点的功能,就是断言中的响应断言. 1.响应断言(对返回文字结果进行相应的匹配)右击请求-->添加-->断言-->响应断言-->添加“要 ...
- soapUI-Properties
1.1.1 Properties 1.1.1.1 概述 - Properties Option Description Toolbar 属性TestStep工具栏 Properties Li ...
- [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming
Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...
- LeetCode Python 位操作 1
Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 & ...
- Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class
转自http://www.anylogic.com/anylogic/help/index.jsp?topic=/com.xj.anylogic.help/html/code/Arrays_Colle ...
- 4:7 Struts实现Ajax
不使用插件: 返回数据: 使用插件: Action里面直接给User赋值,然后在前台拿值. type="json":表示返回json对象: root:表示作为跟对象 include ...
- idea 上搭建 Mybatis 逆向工程
网盘地址:https://pan.baidu.com/s/1VAILpdgQbFk9t89eEv_nWQ 提取码:xdyc
- python函数里面,一个*是可变参数的元祖,两个*是可变参数的字典
python的函数中,有时会有类似*args,**keys这样的参数,代表的是可变参数,一个*表示元祖,两个*表示字典,就是说这个函数可以接受任何类型的参数,都不会报错,有些函数为了提高可用性,会加这 ...
- JDBC报错记录
用JDBC连接oracle时 有如下问题: 问题一.java.lang.ClassNotFoundException: oracle.jdbc.driver.oracledriver 解决: 可以在环 ...