[HDU5354]Bipartite Graph(CDQ分治+并查集)
经典动态二分图问题。
考虑solve(l,r)分治成l,mid和mid+1,r。先将区间[mid+1,r]中的点全部加入图中,若此时存在奇环则ans[l..mid]全部为0,否则递归到左边。
递归完左边后将右边的点全部删去,左边点全部加入,按同样的方法处理右边。
判断奇环使用可撤销带权并查集,注意多组数据不要用memset。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
using namespace std; const int N=;
int T,n,m,top,cnt,u,v;
int fa[N],sz[N],d[N],ans[N],h[N],nxt[N],to[N];
struct P{ int a,b,c; }s[*N];
struct S{ int x,y; };
void add(int u,int v){ to[++cnt]=v; nxt[cnt]=h[u]; h[u]=cnt; } void init(){ cnt=; rep(i,,n) fa[i]=i,sz[i]=,d[i]=,h[i]=; } S get(int x){
if (fa[x]==x) return (S){x,};
S t=get(fa[x]); return (S){t.x,t.y^d[x]};
} void uni(int x,int y){
int t1=get(x).y; x=get(x).x;
int t2=get(y).y; y=get(y).x;
if (sz[x]<sz[y]) swap(x,y);
s[++top]=(P){,y,y}; s[++top]=(P){,x,sz[x]}; s[++top]=(P){,y,};
fa[y]=x; d[y]=t1^t2^; sz[x]+=sz[y];
} void cancel(int x){
if (s[x].a==) fa[s[x].b]=s[x].c;
if (s[x].a==) sz[s[x].b]=s[x].c;
if (s[x].a==) d[s[x].b]=s[x].c;
} void solve(int l,int r){
if (l==r) { ans[l]=; return; }
int mid=(l+r)>>,tmp=top; bool flag=;
rep(u,mid+,r)
for (int i=h[u]; i; i=nxt[i]){
int v=to[i];
if (v>=l && v<=mid) continue;
if (get(u).x!=get(v).x) uni(u,v);
else if (get(u).y==get(v).y) { flag=; break; }
}
if (flag) rep(i,l,mid) ans[i]=; else solve(l,mid);
while (top>tmp) cancel(top--); flag=;
rep(u,l,mid)
for (int i=h[u]; i; i=nxt[i]){
int v=to[i];
if (v>mid && v<=r) continue;
if (get(u).x!=get(v).x) uni(u,v);
else if (get(u).y==get(v).y) { flag=; break; }
}
if (flag) rep(i,mid+,r) ans[i]=; else solve(mid+,r);
while (top>tmp) cancel(top--);
} int main(){
freopen("hdu5354.in","r",stdin);
freopen("hdu5354.out","w",stdout);
for (scanf("%d",&T); T--; ){
scanf("%d%d",&n,&m); init();
rep(i,,m) scanf("%d%d",&u,&v),add(u,v),add(v,u);
solve(,n);
rep(i,,n) printf("%d",ans[i]); puts("");
}
return ;
}
[HDU5354]Bipartite Graph(CDQ分治+并查集)的更多相关文章
- 2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:h ...
- hdu_5354_Bipartite Graph(cdq分治+并查集判二分图)
题目链接:hdu_5354_Bipartite Graph 题意: 给你一个由无向边连接的图,问对于每一个点来说,如果删除这个点,剩下的点能不能构成一个二分图. 题解: 如果每次排除一个点然后去DFS ...
- 【openjudge】C15C Rabbit's Festival CDQ分治+并查集
题目链接:http://poj.openjudge.cn/practice/C15C/ 题意:n 点 m 边 k 天.每条边在某一天会消失(仅仅那一天消失).问每一天有多少对点可以相互到达. 解法:开 ...
- BZOJ 4025: 二分图 [线段树CDQ分治 并查集]
4025: 二分图 题意:加入边,删除边,查询当前图是否为二分图 本来想练lct,然后发现了线段树分治的做法,感觉好厉害. lct做法的核心就是维护删除时间的最大生成树 首先口胡一个分块做法,和hno ...
- 2018.10.01 bzoj3237: [Ahoi2013]连通图(cdq分治+并查集)
传送门 cdq分治好题. 对于一条边,如果加上它刚好连通的话,那么删掉它会有两个大集合A,B.于是我们先将B中禁用的边连上,把A中禁用的边禁用,再递归处理A:然后把A中禁用的边连上,把B中禁用的边禁用 ...
- 【CF603E】Pastoral Oddities cdq分治+并查集
[CF603E]Pastoral Oddities 题意:有n个点,依次加入m条边权为$l_i$的无向边,每次加入后询问:当前图是否存在一个生成子图,满足所有点的度数都是奇数.如果有,输出这个生成子图 ...
- 2018.09.30 bzoj4025: 二分图(线段树分治+并查集)
传送门 线段树分治好题. 这道题实际上有很多不同的做法: cdq分治. lct. - 而我学习了dzyo的线段树分治+并查集写法. 所谓线段树分治就是先把操作分成lognlognlogn个连续不相交的 ...
- hdu5354 Bipartite Graph
分治+并查集.假设要求[L,mid]的答案,那么很明显,如果一条边的两个端点都>mid的话或者一个端点>mid一个端点<L,说明询问[L,mid]这个区间中任何一点时候,这一条边都是 ...
- 【CF576E】Painting Edges 线段树按时间分治+并查集
[CF576E]Painting Edges 题意:给你一张n个点,m条边的无向图,每条边是k种颜色中的一种,满足所有颜色相同的边内部形成一个二分图.有q个询问,每次询问给出a,b代表将编号为a的边染 ...
随机推荐
- bzoj 1046 LIS
假设我们知道以每个点开始到最后的最长上升序列,设为w[i],这样首先我们在w值中取max,如果询问的值比max大,这样显然就是无解,如果小的话,我们需要求出来字典序最小的方案. 那么对于所有i,我们肯 ...
- I题 hdu 1234 开门人和关门人
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1234 开门人和关门人 Time Limit: 2000/1000 MS (Java/Others) ...
- 【Python学习】Jupyter解决单个变量输出问题
使用Jupyter的时候有时候发现,我明明写了好几个变量打印,但是它只显示最后一个.Out只有一个. 但是使用下面的语句.就可以实现多个输出. from IPython.core.interactiv ...
- Vue组件-动态组件
动态组件 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以让多个组件使用同一个挂载点,并动态切换: <div id="app6"& ...
- C函数前向声明省略参数
这样的不带参数的函数声明,在c中是合法的,表示任意参数:当然我们自己写代码最好不要这样写了,但是读老代码还是会遇到: #include <stdio.h> void fun(); int ...
- 如何用jQuery获得radio的值
如何获得radio的值,在网上查了一下,下面总结几种解决方法,. 1.获取选中值: $('input:radio:checked').val(): $("input[type='radio' ...
- [Deep dig] ViewController初始化过程调查
代码:https://github.com/xufeng79x/ViewControllerLife 1.简介: 介绍xib方式.storyborad方式以及code方式下ViewController ...
- 使用while循环遍历文件
/* 使用while循环遍历文件*/ [root@localhost test1]# vim 17.py //add #!/usr/bin/python ll = open('/tmp/1.txt') ...
- 内核添加USB模块
Device Drivers->SCSI device support->SCSI disk support Device Drivers->USB support->Supp ...
- google fcm 推送的流程
总结:1.给一个人推,能成功,2.给多个人推,有两种,一种是给组推,一种是给主题推,之前用的是组推,但是不成功,这里换成主题推: <?phpnamespace App\Http\Controll ...