题意:给一个二分图,问想让二分图变成完全二分图最多能加多少条边。

解法:图染色+dp+bitset优化。设最终的完全二分图两部分点集为A和B,A中点个数为x,B中点个数为y,边数则为x × y,答案即为x × y - m,那么用dp计算集合A中点个数的可能性。先用图染色计算每个连通分量里两种颜色点的个数,用dp[i][j]表示加入第i个连通分量时A集合中有j个点的可能性,可能为1,不可能为0,设联通分量为p,可以得到转移方程为dp[i][j] = dp[i - 1][j - p[i][0]] | dp[i - 1][j - p[i][1]],这样的复杂度为O(n ^ 2),使用bitset可以优化,bitset即为一个二进制数集,即得到dp[i] = (dp[i - 1] << p[i][0]) | (dp[i - 1] << p[i][1])。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<bitset>
#define LL long long
using namespace std;
vector <int> v[10005];
int n, m;
int cnt;
int p[10005][2];
bool vis[10005];
bool color[10005];
queue <int> q;
bitset <10005> dp;
void bfs(int u)
{
while(!q.empty())
q.pop();
int col[2] = {0};
color[u] = 0;
col[0] = 1;
q.push(u);
while(!q.empty())
{
int tmp = q.front();
q.pop();
int len = v[tmp].size();
for(int i = 0; i < len; i++)
{
if(!vis[v[tmp][i]])
{
vis[v[tmp][i]] = true;
color[v[tmp][i]] = !color[tmp];
col[color[v[tmp][i]]]++;
q.push(v[tmp][i]);
}
}
}
p[cnt][0] = col[0];
p[cnt++][1] = col[1];
}
int main()
{
int T;
while(~scanf("%d", &T))
{
while(T--)
{
for(int i = 0; i < 10005; i++)
v[i].clear();
cnt = 0;
memset(p, 0, sizeof p);
memset(vis, 0, sizeof vis);
memset(color, -1, sizeof color);
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
vis[i] = true;
bfs(i);
}
}
dp.reset();
dp[0] = 1;
for(int i = 0; i < cnt; i++)
dp = (dp << p[i][0]) | (dp << p[i][1]);
int ans = 0;
for(int i = 0; i <= n; i++)
{
if(dp[i])
ans = max(ans, i * (n - i));
}
printf("%d\n", ans - m);
}
}
return 0;
}

  

HDU 5313 Bipartite Graph的更多相关文章

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

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

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

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

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

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

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

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

  5. 2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:h ...

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

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

  7. HDU 6321 Dynamic Graph Matching

    HDU 6321 Dynamic Graph Matching (状压DP) Problem C. Dynamic Graph Matching Time Limit: 8000/4000 MS (J ...

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

  9. CodeForces - 600F Edge coloring of bipartite graph

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

随机推荐

  1. Sugarcrm Email Integration

    Introdurce http://www.sugarcrm.com/feature/email-integration Tutor http://www.youtube.com/watch?v=BQ ...

  2. ubuntu系统使用快捷键打开终端方式总结

    ctrl + alt + T 三键齐下打开虚拟终端 ctrl + alt + F1 (~F6)打开系统终端ctrl + alt + F7 返回图形界面都可以使用exit命令关闭, 不同的是虚拟终端ex ...

  3. 我的WebX框架学习总结与心得分享

    最近学习了webx框架, 利用博客园跟大家分享一下自己的学习心得; 周建旭 2014-08-21 网上关于webx的资料少的可怜, 怎么办?  这种情况下不用去求助别人求人只会耽误时间, 不用畏惧; ...

  4. 返回canceled 代码 的原因

    ajax 不支持跨域操作jsonp,才导致返回canceled 代码. 解决的办法就是 <script>标签.jquery 也为我们提供了$.Ajax()方法或$.getScript()方 ...

  5. Unity3D 自动打包整个项目(以AssetBundle实现)

    原地址:http://blog.csdn.net/huang7jiao/article/details/18370653 需求: 在移动开发中,手动控制资源的加载.释放和热更新,是很有必要的. 而Un ...

  6. 盘点 OSX 上最佳的 DevOps 工具

    [编者按]对于运维人员来说,他们往往需要各种各样的工具来应对工作需求,近日 Dustin Collins 通过「The Best DevOps Tools on OSX」一文对 OSX 平台上的工具进 ...

  7. POJ 1061 青蛙的约会(扩展欧几里德)

    点我看题目 题意 : 中文题不详述. 思路 : 设经过s步后两青蛙相遇,则必满足(x+m*s)-(y+n*s) = K*L(k = 0,1,2....) 变形得:(n-m)*s+K*L = x-y ; ...

  8. POJ 3252 Round Numbers(数位dp)

    题意:给定区间[l,r],l < r ,求区间中满足条件的正整数的个数:二进制表示下0的个数不少于1的个数. 分析:f(x)表示<=x时满足条件的数的个数,所求问题即为f(r)-f(l-1 ...

  9. iOS开发UI篇—程序启动原理和UIApplication1

    iOS开发UI篇—程序启动原理和UIApplication   一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就 ...

  10. Linux下Keepalived 安装与配置

    Keepalived 安装与配置 一.环境说明 1.操作系统内核版本:2.6.9-78.ELsmp 2.Keepalived软件版本:keepalived-1.1.20.tar.gz 二.环境配置 1 ...