这题一眼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+并查集)的更多相关文章

  1. bzoj3237 cdq分治+可撤销并查集

    https://www.lydsy.com/JudgeOnline/problem.php?id=3237 年轻的花花一直觉得cdq分治只能用来降维,不料竟然可以用来分治询问 N<=100000 ...

  2. 2018.10.01 bzoj3237: [Ahoi2013]连通图(cdq分治+并查集)

    传送门 cdq分治好题. 对于一条边,如果加上它刚好连通的话,那么删掉它会有两个大集合A,B.于是我们先将B中禁用的边连上,把A中禁用的边禁用,再递归处理A:然后把A中禁用的边连上,把B中禁用的边禁用 ...

  3. [BZOJ3237][AHOI2013]连通图(分治并查集)

    3237: [Ahoi2013]连通图 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1736  Solved: 655[Submit][Status ...

  4. hdu_5354_Bipartite Graph(cdq分治+并查集判二分图)

    题目链接:hdu_5354_Bipartite Graph 题意: 给你一个由无向边连接的图,问对于每一个点来说,如果删除这个点,剩下的点能不能构成一个二分图. 题解: 如果每次排除一个点然后去DFS ...

  5. BZOJ 4025: 二分图 [线段树CDQ分治 并查集]

    4025: 二分图 题意:加入边,删除边,查询当前图是否为二分图 本来想练lct,然后发现了线段树分治的做法,感觉好厉害. lct做法的核心就是维护删除时间的最大生成树 首先口胡一个分块做法,和hno ...

  6. 2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:h ...

  7. 【openjudge】C15C Rabbit's Festival CDQ分治+并查集

    题目链接:http://poj.openjudge.cn/practice/C15C/ 题意:n 点 m 边 k 天.每条边在某一天会消失(仅仅那一天消失).问每一天有多少对点可以相互到达. 解法:开 ...

  8. Codeforces 938G(cdq分治+可撤销并查集+线性基)

    题意: 有一个无向连通图,支持三个操作: 1 x y d : 新建一条x和y的无向边,长度为d 2 x y    :删除x和y之间的无向边 3 x y    :询问x到y的所有路径中(可以绕环)最短的 ...

  9. 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 ...

随机推荐

  1. Jmeter固定定时器(Constant Timer)

    如上图,Constant Timer是jmeter固定定时器元件 一般用来设置延时的,放在某个请求下,表示Constant Timer配置的指定时间后,再开始发起这个请求操作(单位:毫秒) 根据Con ...

  2. php 配置xdebug

    https://blog.csdn.net/Alan8865/article/details/81331252

  3. Floyd-Warshall算法计算有向图的传递闭包

    Floyd-Warshall算法是用来求解所有结点对最短路径的知名算法,其还有一个重要的用途就是求解有向图的传递闭包,下面就让我来介绍算法导论中关于有向图闭包计算的有关记载吧. 有向图的传递闭包:我们 ...

  4. SpringBoot集成MyBatis的分页插件 PageHelper

    首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...

  5. MySQL8.0安装

    背景 MySQl 8.0 出来已经有段时间了,据说性能有很大提高,在网上看过很多安装教程,大同小异, 在这里亲身实战实战下MySQL8.0在Windows10系统下的安装,以下为详细的安装步骤. 1. ...

  6. win10修改TXT文件的关联软件

    打开注册表,按下面路径找: HKEY_CLASSES_ROOT -> txtfile -> shell -> open -> command 在右边可以看到一个默认文件,原来的 ...

  7. Luogu3579 Solar Panels

    整除分块枚举... 真的没有想到会这么简单. 要使一个数 \(p\) 满足 条件, 则 存在\(x, y\), \(a<=x \times p<=b\ \&\&\ c< ...

  8. Eclipse 新建 Maven web 项目

    File --> New --> Maven Project --> 选择存放路径 --> 选择骨架 --> 输入Maven坐标 --> 初步成型,下面还要配置 1 ...

  9. BCode解码练习

    在学习BT协议中的一个小练习 参考了 https://github.com/airtrack/bitwave 具体B编码解释 可以自行搜索或者参考 这篇文章 bittorrent 学习(一) 种子文件 ...

  10. STS中applicationContext.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...