分治+并查集。假设要求[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的更多相关文章

  1. [HDU5354]Bipartite Graph(CDQ分治+并查集)

    经典动态二分图问题. 考虑solve(l,r)分治成l,mid和mid+1,r.先将区间[mid+1,r]中的点全部加入图中,若此时存在奇环则ans[l..mid]全部为0,否则递归到左边. 递归完左 ...

  2. 二分图点染色 BestCoder 1st Anniversary($) 1004 Bipartite Graph

    题目传送门 /* 二分图点染色:这题就是将点分成两个集合就可以了,点染色用dfs做, 剩下的点放到点少的集合里去 官方解答:首先二分图可以分成两类点X和Y, 完全二分图的边数就是|X|*|Y|.我们的 ...

  3. hdu 5313 Bipartite Graph(dfs染色 或者 并查集)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  4. 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 ...

  5. HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】

    Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. CodeForces - 600F Edge coloring of bipartite graph

    Discription You are given an undirected bipartite graph without multiple edges. You should paint the ...

  7. HDU 5313 Bipartite Graph(二分图染色+01背包水过)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  8. HDU 5313 Bipartite Graph (二分图着色,dp)

    题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...

  9. HDU 5313 Bipartite Graph

    题意:给一个二分图,问想让二分图变成完全二分图最多能加多少条边. 解法:图染色+dp+bitset优化.设最终的完全二分图两部分点集为A和B,A中点个数为x,B中点个数为y,边数则为x × y,答案即 ...

随机推荐

  1. URL编码数据转换为JSON数据

    NSString *urlString; urlString=[self    URLDecodedString:urlString]; -(NSString *)URLDecodedString:( ...

  2. js判断ie版本号

     jQuery 2.0 去除了对浏览器版本号的判断(它推荐特性检测),这里是一个老外写的原生判断方法,这段代码着实巧妙!既简介.有向后兼容!一般做法都是:正则搜索 USER_AGENT :但因为历史原 ...

  3. PHP---关联模型

    MANY_TO_MANY

  4. php--列表展示(小实训一月考)

    效果图:

  5. Asp.net Session保存到Redis: 使用 RedisSessionStateProvider

    Install-Package Microsoft.Web.RedisSessionStateProvider 依赖于: Dependencies StackExchange.Redis.Strong ...

  6. angularJS自定义指令scope代替link

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. android Listview item 中有button,item就不响应触摸事件

    为button设置 beanButton.getButton().setFocusable(false); beanButton.getButton().setFocusableInTouchMode ...

  8. LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1090 题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾 ...

  9. 点击空白处隐藏div

    class="bigPic"的div是被显示或隐藏的div,另外.case > ul > li是class="bigPic"的父级元素 $(&quo ...

  10. iOS:特殊符号大全

    特殊符号大全分享给大家,直接复制粘贴就可以使用了! ░ ▒ ▬ ♦ ◊ ◦ ♠ ♣ ▣ ۰•● ❤ ●•۰ ► ◄ ▧ ▨ ♨ ◐ ◑ ↔ ↕ ▪ ▫ ☼ ♦ ♧♡♂♀♠♣♥❤☜☞☎☏⊙◎ ☺☻☼▧▨ ...