bzoj3237(cdq+并查集)
这题一眼lct,然而
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int n,m,k,fa[maxn],sta1[maxn*],sta2[maxn*],top,tt,ans[maxn];
int getfa(int x){
if(x!=fa[x]){
sta1[++top]=x;sta2[top]=fa[x];
fa[x]=getfa(fa[x]);
}
return fa[x];
}
struct edg{
int x,y,tim;
}e[maxn*];
struct que{
int num,c[];
}q[maxn];
void cdq(int l,int r){
int now=top;
if(l==r){
ans[l]=;
for(int i=;i<=q[l].num;++i){
int fx=getfa(e[q[l].c[i]].x);
int fy=getfa(e[q[l].c[i]].y);
if(fx!=fy){
ans[l]=;break;
}
}
while(top!=now)fa[sta1[top]]=sta2[top],top--;
return;
}
++tt;
int mid=l+r>>;
for(int i=l;i<=mid;++i)
for(int j=;j<=q[i].num;++j){
e[q[i].c[j]].tim=tt;
}
for(int i=mid+;i<=r;++i)
for(int j=;j<=q[i].num;++j)
if(e[q[i].c[j]].tim!=tt){
int fx=getfa(e[q[i].c[j]].x);
int fy=getfa(e[q[i].c[j]].y);
if(fx!=fy){
sta1[++top]=fx;sta2[top]=fa[fx];
fa[fx]=fy;
}
}
cdq(l,mid);
while(top!=now){fa[sta1[top]]=sta2[top];top--;}
++tt;
for(int i=mid+;i<=r;++i)
for(int j=;j<=q[i].num;++j){
e[q[i].c[j]].tim=tt;
}
for(int i=l;i<=mid;++i)
for(int j=;j<=q[i].num;++j)
if(e[q[i].c[j]].tim!=tt){//把后面的所有边中前面没删的加上;
int fx=getfa(e[q[i].c[j]].x);
int fy=getfa(e[q[i].c[j]].y);
if(fx!=fy){
sta1[++top]=fx;sta2[top]=fa[fx];
fa[fx]=fy;
}
}
cdq(mid+,r);
}
int main(){
cin>>n>>m;
for(int i=;i<=n;++i)fa[i]=i;
for(int i=;i<=m;++i){
scanf("%d%d",&e[i].x,&e[i].y);
e[i].tim=;
}
cin>>k;tt=;
for(int i=;i<=k;++i){
scanf("%d",&q[i].num);
for(int j=;j<=q[i].num;++j){
scanf("%d",&q[i].c[j]);
e[q[i].c[j]].tim=tt;
}
}
for(int i=;i<=m;++i)
if(e[i].tim!=tt){
int fx=getfa(e[i].x);
int fy=getfa(e[i].y);
if(fx!=fy)fa[fx]=fy;
}
cdq(,k);
for(int i=;i<=k;++i){
if(ans[i])puts("Connected");
else puts("Disconnected");
}
return ;
}
题解说可以cdq+并查集,于是复习了一下cdq;
bzoj3237(cdq+并查集)的更多相关文章
- bzoj3237 cdq分治+可撤销并查集
https://www.lydsy.com/JudgeOnline/problem.php?id=3237 年轻的花花一直觉得cdq分治只能用来降维,不料竟然可以用来分治询问 N<=100000 ...
- 2018.10.01 bzoj3237: [Ahoi2013]连通图(cdq分治+并查集)
传送门 cdq分治好题. 对于一条边,如果加上它刚好连通的话,那么删掉它会有两个大集合A,B.于是我们先将B中禁用的边连上,把A中禁用的边禁用,再递归处理A:然后把A中禁用的边连上,把B中禁用的边禁用 ...
- [BZOJ3237][AHOI2013]连通图(分治并查集)
3237: [Ahoi2013]连通图 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1736 Solved: 655[Submit][Status ...
- hdu_5354_Bipartite Graph(cdq分治+并查集判二分图)
题目链接:hdu_5354_Bipartite Graph 题意: 给你一个由无向边连接的图,问对于每一个点来说,如果删除这个点,剩下的点能不能构成一个二分图. 题解: 如果每次排除一个点然后去DFS ...
- BZOJ 4025: 二分图 [线段树CDQ分治 并查集]
4025: 二分图 题意:加入边,删除边,查询当前图是否为二分图 本来想练lct,然后发现了线段树分治的做法,感觉好厉害. lct做法的核心就是维护删除时间的最大生成树 首先口胡一个分块做法,和hno ...
- 2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:h ...
- 【openjudge】C15C Rabbit's Festival CDQ分治+并查集
题目链接:http://poj.openjudge.cn/practice/C15C/ 题意:n 点 m 边 k 天.每条边在某一天会消失(仅仅那一天消失).问每一天有多少对点可以相互到达. 解法:开 ...
- Codeforces 938G(cdq分治+可撤销并查集+线性基)
题意: 有一个无向连通图,支持三个操作: 1 x y d : 新建一条x和y的无向边,长度为d 2 x y :删除x和y之间的无向边 3 x y :询问x到y的所有路径中(可以绕环)最短的 ...
- 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 ...
随机推荐
- spark内存分配
问题描述 在测试spark on yarn时,发现一些内存分配上的问题,具体如下. 在$SPARK_HOME/conf/spark-env.sh中配置如下参数: SPARK_EXECUTOR_INST ...
- Linux - 系统信息相关命令
系统信息相关命令 本节内容主要是为了方便通过远程终端维护服务器时,查看服务器上当前 系统日期和时间 / 磁盘空间占用情况 / 程序执行情况 本小结学习的终端命令基本都是查询命令,通过这些命令对系统资源 ...
- Babel 配置选项
comments 是否去掉注释,true(默认)/false.
- CentOS 7 安装与卸载MySQL 5.7
先介绍卸载 防止重装 yum方式 查看yum是否安装过mysql yum list installed mysql* 如或显示了列表,说明系统中有MySQL yum卸载 根据列表上的名字 yum re ...
- Hillstone设备管理-恢复出厂设置
1.CLI命令行操作 unset all: 根据提示选择是否保存当前配置y/n: 选择是否重启y/n: 系统重启后即恢复到出厂设置. 2.webUI操作 “系统”—“配置”,点击“清除”按钮,系统会提 ...
- 记忆化搜索 P1464 Function
题目描述 对于一个递归函数w(a,b,c) 如果a≤0 or b≤0 or c≤0就返回值1. 如果a>20 or b>20 or c>20就返回w(20,20,20) 如果a< ...
- iOS多图上传
iOS多图上传涉及到多线程问题,个人比较喜欢使用GCD操作,下边是最近写的一个多图上传代码,附带相关注释 __block BOOL allSucc = YES; __block int m = 0; ...
- Vue 获取元素样式 元素高度
看到这个问题我第一时间想的竟然是JS 不知道你是怎么想的 不过昨天有一个小哥哥 问我一个Vue的 哈哈哈 get了 我当时问他为什么不用JS获取 他说 这个性能更高 那我们来看看这个高性能的获取元素高 ...
- lodash 判断一个数据是否包含另一个数组
if (_.intersection(v.ids, value).length == value.length) { this.groupListExtData.push(v.names); } ...
- nuget安装本地nupkg文件
打开visual studio,菜单选择‘工具’->‘选项’ 然后 接下来,选择‘程序包源’,把‘包括预发行版’打钩,然后安装需要的包到工程即可,如下图: