【XSY2469】graph 分治 并查集
来源:互联网 发布:unity3d射击游戏项目 编辑:程序博客网 时间:2024/05/17 23:27
题目大意
给你一张
题解
一个图是二分图
可以用并查集,维护每个点到根的距离
如果删除
考虑分治,对于区间
因为有撤销操作,所以要用按秩合并的并查集
时间复杂度:
代码
#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#include<ctime>#include<utility>using namespace std;typedef long long ll;typedef pair<int,int> pii;struct list{ int v[200010]; int t[200010]; int h[100010]; int n; void clear() { memset(h,0,sizeof h); n=0; } void add(int x,int y) { n++; v[n]=y; t[n]=h[x]; h[x]=n; }};list li;int f[100010];int s[100010];int d[100010];int find(int x){ return f[x]==x?x:find(f[x]);}int getdist(int x){ return f[x]==x?0:getdist(f[x])^d[x];}int e1[100010];int e2[100010];int top;int ans[100010];int merge(int x,int y){ int dist=getdist(x)^getdist(y)^1; if((x=find(x))==(y=find(y))) return dist; top++; if(s[x]<=s[y]) { e1[++top]=x; e2[top]=y; d[x]=dist; s[y]+=s[x]; f[x]=y; } else { e1[++top]=y; e2[top]=x; d[y]=dist; s[x]+=s[y]; f[y]=x; } return 0;}void solve(int l,int r){ if(l==r) { ans[l]=1; return; } int mid=(l+r)>>1; int now=top; int i,j; int b=1; for(i=l;i<=mid&&b;i++) for(j=li.h[i];j&&b;j=li.t[j]) if(li.v[j]<=mid||li.v[j]>r) b^=merge(i,li.v[j]); if(b) solve(mid+1,r); else for(i=mid+1;i<=r;i++) ans[i]=0; while(top>now) { f[e1[top]]=e1[top]; s[e2[top]]-=s[e1[top]]; top--; } now=top; b=1; for(i=mid+1;i<=r&&b;i++) for(j=li.h[i];j&&b;j=li.t[j]) if(li.v[j]<l||li.v[j]>mid) b^=merge(i,li.v[j]); if(b) solve(l,mid); else for(i=l;i<=mid;i++) ans[i]=0; while(top>now) { f[e1[top]]=e1[top]; s[e2[top]]-=s[e1[top]]; top--; }}void solve(){ top=0; int n,m; scanf("%d%d",&n,&m); int i; int x,y; li.clear(); for(i=1;i<=m;i++) { scanf("%d%d",&x,&y); li.add(x,y); li.add(y,x); } for(i=1;i<=n;i++) { f[i]=i; s[i]=1; } solve(1,n); for(i=1;i<=n;i++) putchar(ans[i]+'0'); putchar('\n');}int main(){// freopen("c.in","r",stdin);// freopen("c.out","w",stdout); int t; scanf("%d",&t); while(t--) solve(); return 0;}
阅读全文
0 0
- 【XSY2469】graph 分治 并查集
- 【hdu 5354】Bipartite Graph【分治 并查集】
- 【hdu】5354 Bipartite Graph【cdq分治+并查集】
- HDU 5354 Bipartite Graph 分治,并查集
- JZOJ4769 【GDOI2017模拟9.9】graph CDQ分治+用按秩合并维护带撤销的并查集(BZOJ 4025)
- HDU5631--Rikka with Graph--并查集
- hdoj5631Rikka with Graph【并查集】
- Rikka with Graph(并查集)
- hihocoder1629:Graph (分块+并查集)
- 4025: 二分图 分治+并查集
- 分治时间+可撤销并查集
- [ CDQ分治 并查集 ][ AHOI2013 ] BZOJ3237
- Sicily 1949 && 1876 Basic Graph Problem(RMQ+并查集)
- HDU 3560 Graph’s Cycle Component 【并查集】
- POJ 3834 Graph Game 博弈 dfs+并查集剪枝
- Codeforces 9E Interesting Graph and Apples(并查集)
- HDU 3560 并查集 Graph’s Cycle Component
- LA 5031 Graph and Queries (Treap + 并查集)
- 表单验证,彭..
- poj_3660_Cow Contest
- eclipse查找的几种方法
- 关于对象的小细节
- 选择数据库SQLite还是MySQL?
- 【XSY2469】graph 分治 并查集
- 多用户linux下安装tensorflow、keras环境;如何在系统自带python和Anaconda间切换
- 深入理解计算机系统(序章)------谈程序员为什么要懂底层计算机结构
- 申硕统考难不难?
- Adaptive Icons
- 编译出现:While reading /Users/apple/Downloads/xxx/BgImage.png pngcrush caught libpng error?
- MFC——模态对话框和非模态对话框区别
- git 作者Seeker-Wu
- 安卓测试工程师monkey的基本使用----小白版(二)