hdu5354 Bipartite Graph
分治+并查集。假设要求[L,mid]的答案,那么很明显,如果一条边的两个端点都>mid的话或者一个端点>mid一个端点<L,说明询问[L,mid]这个区间中任何一点时候,这一条边都是连接的,否则的话递归下去处理。[mid+1,R]同理。
一个图不是二分图的话说明存在奇环,奇环可以用并查集处理。这里的并查集不使用路径压缩而使用按轶合并。并查集的还原操作可以用一个栈记录每次合并时的具体操作,然后按序还原即可。
代码
#include<cstdio>
#include<vector>
#define N 300010
using namespace std;
int f[N],d[N];
int n,m,i,dp;
int tt[N],pre[N],p[N],ans[N],flag[N];
int tot,stack[N],a[N],b[N],g[N];
vector<int> vec[N];
void link(int x,int y)
{
dp++;pre[dp]=p[x];p[x]=dp;tt[dp]=y;
}
int getfa(int x,int& y)
{
y=;
while (x!=f[x])
{
y^=g[x];
x=f[x];
}
return x;
}
void Union(int x,int y,int &z)
{
int disx,disy;
x=getfa(x,disx);
y=getfa(y,disy);
if (x!=y)
{
if (d[x]>d[y]) x^=y^=x^=y;
f[x]=y;
g[x]=(disx^disy^);
tot++;stack[tot]=x;
if (d[x]==d[y])
{
flag[tot]=;
d[y]++;
}
else
flag[tot]=;
}
else
{
if (disx^disy^)
z|=;
}
}
void recover(int x)
{
while (tot>x)
{
if (flag[tot])
d[f[stack[tot]]]--;
f[stack[tot]]=stack[tot];
g[stack[tot]]=;
tot--;
}
}
void solve(int x,int l,int r,int q)
{
int i,tmp,len,t,Ans;
if (l==r)
{
ans[l]=-q;
return;
}
int mid=(l+r)>>; vec[*x].clear();
vec[*x+].clear();
tmp=tot;
Ans=q;
len=vec[x].size();
for (i=;i<len;i++)
{
t=vec[x][i];
if ((a[t]>mid)||((a[t]<l)&&(b[t]>mid)))
Union(a[t],b[t],Ans);
else
vec[*x].push_back(t);
}
solve(*x,l,mid,Ans); Ans=q;
recover(tmp);
for (i=;i<len;i++)
{
t=vec[x][i];
if ((b[t]<mid+)||((a[t]<mid+)&&(b[t]>r)))
Union(a[t],b[t],Ans);
else
vec[*x+].push_back(t);
}
solve(*x+,mid+,r,Ans);
}
int main()
{
int tt;
scanf("%d",&tt);
while (tt)
{
tt--;
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
{
g[i]=;
f[i]=i;
}
tot=;
vec[].clear();
for (i=;i<=m;i++)
{
scanf("%d%d",&a[i],&b[i]);
if (a[i]>b[i])
a[i]^=b[i]^=a[i]^=b[i];
vec[].push_back(i);
}
solve(,,n,);
for (i=;i<=n;i++)
printf("%d",ans[i]);
printf("\n");
}
}
hdu5354 Bipartite Graph的更多相关文章
- [HDU5354]Bipartite Graph(CDQ分治+并查集)
经典动态二分图问题. 考虑solve(l,r)分治成l,mid和mid+1,r.先将区间[mid+1,r]中的点全部加入图中,若此时存在奇环则ans[l..mid]全部为0,否则递归到左边. 递归完左 ...
- 二分图点染色 BestCoder 1st Anniversary($) 1004 Bipartite Graph
题目传送门 /* 二分图点染色:这题就是将点分成两个集合就可以了,点染色用dfs做, 剩下的点放到点少的集合里去 官方解答:首先二分图可以分成两类点X和Y, 完全二分图的边数就是|X|*|Y|.我们的 ...
- hdu 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- Learning Query and Document Similarities from Click-through Bipartite Graph with Metadata
读了一篇paper,MSRA的Wei Wu的一篇<Learning Query and Document Similarities from Click-through Bipartite Gr ...
- HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
Bipartite Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- CodeForces - 600F Edge coloring of bipartite graph
Discription You are given an undirected bipartite graph without multiple edges. You should paint the ...
- HDU 5313 Bipartite Graph(二分图染色+01背包水过)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- HDU 5313 Bipartite Graph (二分图着色,dp)
题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...
- HDU 5313 Bipartite Graph
题意:给一个二分图,问想让二分图变成完全二分图最多能加多少条边. 解法:图染色+dp+bitset优化.设最终的完全二分图两部分点集为A和B,A中点个数为x,B中点个数为y,边数则为x × y,答案即 ...
随机推荐
- nginx博客系统(内含nginx图片缩略图处理代码,不错)
一直以来都在Qzone.CSDN等上面写博客,偶尔有些想法就在Paas平台上搭建服务,新浪和曾经的google上都用过其appengine.可是在别人的平台上写东西,总归有些不方便,有受制于人的感觉. ...
- 在Windows 环境下编译Qt静态库(QT5.32)
参考链接 Qt5.3 Tools and Versions MinGW ICU ActivePerl Qt 安装MinGW工具链环境 这里在Win32环境下要安装一个MinGW工具链,这里最好是先安装 ...
- Mongoose中关联查询populate的使用
MongoDB中没有join的特性,因此无法使用join进行表的连接和关联查询,在Mongoose中封装了populate方法,在定义一个 Schema 的时候可以指定了其中的字段(属性)是另一个Sc ...
- 1069 Nim游戏
1069 Nim游戏 基准时间限制:1 秒 空间限制:131072 KB 有N堆石子.A B两个人轮流拿,A先拿.每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜.假设A ...
- 1001 数组中和等于K的数对
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A ...
- Android笔记:string-array数据
把相应的数据放到values文件夹的arrays.xml文件里 String数组 <?xml version="1.0" encoding="utf-8" ...
- 我的工具箱之Opera浏览器
下载地址:http://pan.baidu.com/s/1gdVQA11 刚出来时挺火的,后来有点渐趋式微了.
- weizmann数据库
http://www.wisdom.weizmann.ac.il/~vision/SpaceTimeActions.html
- Android 关于ExpandableListView控件setOnChildClickListener无效问题
其实很简单,在适配器里面重写isChildSelectable的时候返回值切记为true,这样才能使得二级监听有响应. 其次注意继承的是BaseExpandableListAdapter
- 智能生活 科技无限 CTO VOICE 第二期 智能硬件创新创业专场演讲嘉宾招募
生活不只有诗和远方,还有当下的痛点和需求 当可穿戴设备.虚拟现实.无人机.机器人进入人们视线甚至生活当中 下一个风口就在智能硬件领域上凸显 那么,创业者如何撕掉智能外衣,设计一款有竞争力的智能硬件? ...