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

解法:图染色+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. Mvc生命周期深度剖析

    客户端发送请求->IIS, UrlRouting模块对比URL, 默认如果该URL能对应到实体文件则退出MVC管道把控制权交还给IIS. 如果RegisterRoutes中的路由规则对比成功默认 ...

  2. JQuery 知识点

    1:animate 动画效果 $(function () { $(".sidebar-nav a").mouseover(function () { $(this).animate ...

  3. FlushMode属性与transaction(spring注入的事务)

    一.参见hibernate的api http://tool.oschina.net/apidocs/apidoc?api=hibernate-3.6.10 http://tool.oschina.ne ...

  4. [转载]VS2012程序打包部署详解

    上篇博客把收费系统的总体设计进行了一遍讲解,讲解的同时掺杂了些有关.NET编译机制的总结.程序编写测试完成后接下来我们要做的是打包部署程序,但VS2012让人心痛的是没有了打包工具.不知道出于什么原因 ...

  5. [转载]JS对URL的编码

    虽然escape().encodeURI().encodeURIComponent()三种方法都能对一些影响URL完整性的特殊字符进行过滤.但后两者是将字符串转换为UTF-8的方式来传输,解决了页面编 ...

  6. MySQL分区表(转)

    查看分区情况 SELECT * FROM information_schema.PARTITIONS WHERE table_name='table_name': PARTITION_NAME:分区的 ...

  7. UIcollectionView的使用(首页的搭建2)

    2.2 直接购买的UIcollectionCell 2.2.1创建CFPromptBuyCell,继承自UICollectionViewCell,定义了标题和图片两个属性 2.2.2 在.m文件中定义 ...

  8. 分布式java应用

    大型应用,通常会拆分为多个子系统来实现.       对Java来说,这些子系统可能部署在同一台机器的多个不同的JVM中,也可能部署在不同的机器上,但这些子系统又不是完全独立的,要相互通信来共同实现业 ...

  9. 如何保存ISE综合后的RTL schematic为pdf

    如何保存ISE综合后的RTL schematic为pdf 2013-06-23 20:50:10 代码进行综合后,可以得到一个ngr文件,在ISE中打开该文件可以打开RTL schematic,这样每 ...

  10. CCS使用TIPS

    2013-06-20 09:37:49 CCS使用TIPS: 代码编写: CCS中通过Using CodeSense方便写代码,跟VC助手类似,具体使用方法在ccs的help中搜索using visu ...