【bzoj3237】 Ahoi2013—连通图
http://www.lydsy.com/JudgeOnline/problem.php?id=3237 (题目链接)
题意
给出一个无向图,$Q$组询问,每次询问将原图断掉$C$条边后是否还连通。
Solution
CDQ图分治,并查集维护。实在写不动题了,我能说我是蒯的吗T_T:http://blog.csdn.net/creationaugust/article/details/50889351
细节
bzoj的G++版本是多久以前的了,莫名CE。。
代码
// bzoj3237
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=200010,maxm=5000010;
int ans[maxn],del[maxn],fa[maxn],st[maxm];
int n,m,Q,tim,top;
struct data {int c,id,e[4];}q[maxn];
struct edge {int u,v;}e[maxn]; namespace Unionset {
int find(int x) {
if (fa[x]==x) return x;
st[++top]=x;st[++top]=fa[x];return fa[x]=find(fa[x]);
}
void Union(int u,int v) {
if (find(u)!=find(v)) {
st[++top]=fa[v];
st[++top]=fa[fa[v]];
fa[fa[v]]=fa[u];
}
}
}
using namespace Unionset; void solve(int l,int r) {
int now=top,mid=(l+r)>>1,flag=1;
if (l==r) {
for (int i=0;i<q[l].c && flag;i++)
if (find(e[q[l].e[i]].u)!=find(e[q[l].e[i]].v)) flag=0;
ans[q[l].id]=flag;
for (;now!=top;top-=2) fa[st[top-1]]=st[top];
return;
}
tim++;
for (int i=l;i<=mid;i++)
for (int j=0;j<q[i].c;j++) del[q[i].e[j]]=tim;
for (int i=mid+1;i<=r;i++)
for (int j=0;j<q[i].c;j++)
if (del[q[i].e[j]]!=tim) Union(e[q[i].e[j]].u,e[q[i].e[j]].v);
solve(l,mid);tim++;
for (;now!=top;top-=2) fa[st[top-1]]=st[top];
for (int i=mid+1;i<=r;i++)
for (int j=0;j<q[i].c;j++) del[q[i].e[j]]=tim;
for (int i=l;i<=mid;i++)
for (int j=0;j<q[i].c;j++)
if (del[q[i].e[j]]!=tim) Union(e[q[i].e[j]].u,e[q[i].e[j]].v);
solve(mid+1,r);
for (;now!=top;top-=2) fa[st[top-1]]=st[top];
} int main() {
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) fa[i]=i;
for (int i=1;i<=m;i++) scanf("%d%d",&e[i].u,&e[i].v);
scanf("%d",&Q);tim=1;
for (int i=1;i<=Q;i++) {
scanf("%d",&q[i].c);q[i].id=i;
for (int j=0;j<q[i].c;j++) scanf("%d",&q[i].e[j]),del[q[i].e[j]]=tim;
}
for (int i=1;i<=m;i++) if (del[i]!=tim) Union(e[i].u,e[i].v);
solve(1,Q);
for (int i=1;i<=Q;i++) puts(ans[i] ? "Connected" : "Disconnected");
return 0;
}
【bzoj3237】 Ahoi2013—连通图的更多相关文章
- [BZOJ3237][AHOI2013]连通图(分治并查集)
3237: [Ahoi2013]连通图 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1736 Solved: 655[Submit][Status ...
- bzoj3569 DZY Loves Chinese II & bzoj3237 [AHOI2013] 连通图
给一个无向连通图,多次询问,每次询问给 k 条边,问删除这 k 条边后图的连通性,对于 bzoj3237 可以离线,对于 bzoj3569 强制在线 $n,m,q \leq 500000,k \leq ...
- BZOJ3237: [Ahoi2013]连通图
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3237 cdq分治+缩点. 可以每次处理的时候把除l~r之外的边的端点都连起来.然后去跑cdq分 ...
- BZOJ3237:[AHOI2013]连通图(线段树分治,并查集)
Description Input Output Sample Input 4 5 1 2 2 3 3 4 4 1 2 4 3 1 5 2 2 3 2 1 2 Sample Output Connec ...
- BZOJ3237 AHOI2013连通图(线段树分治+并查集)
把查询看做是在一条时间轴上.那么每条边都有几段存在时间.于是线段树分治就好了. 然而在bzoj上t掉了,不知道是常数大了还是写挂了. 以及brk不知道是啥做数组名过不了编译. #include< ...
- 2018.10.01 bzoj3237: [Ahoi2013]连通图(cdq分治+并查集)
传送门 cdq分治好题. 对于一条边,如果加上它刚好连通的话,那么删掉它会有两个大集合A,B.于是我们先将B中禁用的边连上,把A中禁用的边禁用,再递归处理A:然后把A中禁用的边连上,把B中禁用的边禁用 ...
- BZOJ 3237([Ahoi2013]连通图-cdq图重构-连通性缩点)
3237: [Ahoi2013]连通图 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 106 Solved: 31 [ Submit][ St ...
- BZOJ 3237: [Ahoi2013]连通图
3237: [Ahoi2013]连通图 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1161 Solved: 399[Submit][Status ...
- 线段树分治初步学习&洛谷P5227[AHOI2013]连通图
线段树分治 其实思想说起来是比较简单的,我们把这个题里的所有操作(比如连边删边查询balabala)全部拍到一棵线段树上,然后对着整棵树dfs一下求解答案,顺便把操作做一下,回溯的时候撤销一下即可.虽 ...
随机推荐
- AS3.0 自定义右键菜单类
AS3.0 自定义右键菜单类: /** * 自定义右键菜单类 * 自定义菜单项不得超过15个,每个标题必须至少包含一个可见字符. * 标题字符不能超过100个,并且开头的空白字符会被忽略. * 与任何 ...
- 使用 Vue.js 2.0+ Vue-resource 模仿百度搜索框
使用 Vue.js 2.0 模仿百度搜索框 <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...
- 记录一次Docker For Windows10镜像加速器配置
1.访问https://www.daocloud.io 注册账号 2.访问资源->加速器,或者直接访问网址https://www.daocloud.io/mirror,页面中间有加速配置,例如我 ...
- 深入浅出etcd系列 – 心跳和选举
作者:宝爷 校对:DJ 1.绪论 etcd作为华为云PaaS的核心部件,实现了PaaS大多数组件的数据持久化.集群选举.状态同步等功能.如此重要的一个部件,我们只有深入地理解其架构设计和内部工作机制, ...
- Unity Inspector添加自定义按钮(Button)
在Unity开发游戏的时候,为了有一个更快更方便的工作流,我们往往会在Editor下开发一些方便实用的工具.在工具中,用到最多,最关键的就是按钮,它是工具的首席执行官.下面就用最简单的代码来演示添加一 ...
- [T-ARA][결혼 하지마][不要结婚]
歌词来源:http://music.163.com/#/song?id=27808773 作曲 : 二段横踢 [作曲 : 二段横踢] 作词 : 二段横踢 [作词 : 二段横踢] Hey anybody ...
- ace how to guide
Configuring the editor there are several ways to pass configuration to Ace 有几种方法可以将配置传递给ace // pass ...
- Linux内核分析——计算机是如何工作的
马悦+原创作品转载请注明出处+<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.计算机是如何工作的 ( ...
- MyEclipse同时配置多个tomcat
步骤: 1.可以把原有tomcat复制一份,或者下载新的tomcat,如果有必要的话,修改/conf/service.xml文件中tomcat的端口号,避免端口同时暂用出现错误 2.请看一下图片:打开 ...
- PAT 1064 朋友数
https://pintia.cn/problem-sets/994805260223102976/problems/994805267416334336 如果两个整数各位数字的和是一样的,则被称为是 ...