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,答案即 ...
随机推荐
- UI auto test
java.home/lib/security/java.policy (Solaris/Linux) http://www.cnblogs.com/richaaaard/p/5091059.html ...
- sqlserver中常用的全局变量
变量 Transact-SQL语言中有两种形式的变量,一种是用户自己定义的局部变量,另外一种是系统提供的全局变量.局部变量 局部变量是一个能够拥有特定数据类型的对象,它的作用范围仅限制在程序内部.局部 ...
- mysql binlog恢复
MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...
- TCP的三次握手四次挥手
一.三次握手 1.wireshark 抓包 2.TCP报文手部 注意标志位: 1).同步 SYN = 1 表示这是一个连接请求或连接接受报文. 2).只有当 ACK = 1 时确认号字段才有效.当 A ...
- 系统默认Select框 知多少
<div class="user_base_info_list"><div class="user_base_info_lab">学制: ...
- Is Anchor magento
如何在magento分类页的Layered Navigation中可以用magento后台已有的attributes进行筛选. 首先,进入后台 Catalog > Manage Categori ...
- TortoiseGit 的使用
日常用法 (1) 创建新库 在文件夹中按右键, 选择Git Create repository here 就可以创建库了. 在出现的窗口中, 不勾选选项, 直接按OK 在目录中就会出现一个名为.git ...
- SpringMVC @ResponseBody的使用
原文链接:http://www.jianshu.com/p/7097fea8ce3f@ResponseBody用法作用:该注解用于将Controller的方法返回的对象,根据HTTP Request ...
- 还原ORACLE DUMP 的值
还原DUMP出来的数字SQL> select dump(2000,16) from dual; DUMP(2000,16)------------------Typ=2 Len=2: c2,15 ...
- 简述Mesos API–files
Below is a set of endpoints available on a Mesos agent. These endpoints are reachable at the address ...