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,答案即 ...
随机推荐
- Bootstrap 一. 排版样式(内联文本元素,对齐,大小写,缩略语,地址文本,引用文本,列表排版 ,代码 )
第 2 章 排版样式 在 h1 ~ h6 元素之间,还可以嵌入一个 small 元素作为副标题 <h1>Bootstrap 框架 <small>Bootstrap 小标题< ...
- Moogoose操作之Schema实现增删查改
Schema不仅定义了文档结构和使用性能,可以为后面的Model和Entity提供公共的属性和方法. Schema.Model.Entity的关系: Schema : 可以定义字段类型,不具备数据库的 ...
- 面向对象分析方法(I)
找出最关键的一些业务场景:一般通过动词来寻找,比如招聘系统中,一个应聘人投递一个职位就是一次应聘,应聘就是一个业务场景:一个学生参加某门课的考试,那么考试就是一个业务场景:一个学生去图书馆借书,那么借 ...
- C#类的继承
public class Animal { public string word=""; //virtual表示虚方法,用以被重写 public virtual void say( ...
- LeetCode Find Peak Element
原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...
- [Logstash]使用详解(转)
Logstash是一款轻量级的日志搜集处理框架,可以方便的把分散的.多样化的日志搜集起来,并进行自定义的处理,然后传输到指定的位置,比如某个服务器或者文件. 本文针对官方文档进行翻译以及实践,希望有更 ...
- django工作原理
- 史上最全的iOS面试题及答案
迷途的羔羊--专为路痴量身打造的品牌.史上最精准的定位.想迷路都难!闪电更新中...敬请期待,欢迎提意见.下载地址:https://itunes.apple.com/us/app/mi-tu-de-g ...
- Map小记
Corelocation地理定位Map Kit地图展示 在移动互联网的时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边找餐馆找酒店,找银行,找电影院在上述应用中都用到了地图 ...
- CentOS 配置本地yum源
[root@localhost ~]#ls /media/dvd/ ...