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

解法:图染色+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. 【趟坑】公共引用的jar包 pom的配置方法

    http://www.cnblogs.com/viewcozy/p/4789877.html 接上一篇 ,内部moudule生成jar的方式 上一篇已经实现了,想把jar作为公共的部分让任何项目都可以 ...

  2. 自己实现的android树控件,android TreeView

    1.开发原因 在项目中经常需要一个需要一个树状框架,这是非常常见的控件.不过可能是谷歌考虑到android是手机系统,界面宽度有限, 所以只提供了只有二级的ExpandableListView.虽然这 ...

  3. Linux操作系统工作的基础

    简介: 本文根据 Linux™ 系统工作基础的分析,对存储程序计算机.堆栈(函数调用堆栈)机制和中断机制进行概述.文中将为您提供操作系统(内核)如何工作的细节,进一步从宏观概述结合关键点进行微观(CS ...

  4. BZOJ 3160 万径人踪灭 解题报告

    这个题感觉很神呀.将 FFT 和 Manacher 有机结合在了一起. 首先我们不管那个 “不能连续” 的条件,那么我们就可以求出有多少对字母关于某一条直线对称,然后记 $T_i$ 为关于直线 $i$ ...

  5. hdu 1024

    参考了一下 http://moxi466839201.blog.163.com/blog/static/18003841620110220374942/ 滚动数组   状态转移方程不太好理解 .... ...

  6. 支付宝Unity

    原地址:http://blog.csdn.net/sgnyyy/article/details/20444627 说明:支付宝Android的SDK接入只有一个接口,付费. 1. Android代码的 ...

  7. 认识FiddlerScript

    FiddlerScript 是Fiddler 的一项非常强大的功能,它允许你增强Fiddler UI,添加新的特性,修改请求与响应内容等等... 1.编写FiddlerScript FiddlerSc ...

  8. HDU 1429 胜利大逃亡(续)(三维BFS)

    题目链接 题意 : 中文题不详述. 思路 : 这个题和1885差不多一样的,所以我直接改了改那个代码就交上了,链接 #include <stdio.h> #include <stri ...

  9. codeforces #309 div1 C

    首先我们会发现所有的人构成了一个图 定义相爱为 在一个集合里 定义相恨为 不在一个集合里 很容易发现满足条件的图一定是一个二分图 那么分类讨论如下: 1.如果出现不合法 答案为0 2.如果不是一个二分 ...

  10. PHP开发工具介绍之zendStudio

    1.PHP开发工具介绍之zendStudio 下载:进入官网:http://www.zend.com/en/products/studio 选择下载安装 注意这里的工作空间要和你Apache的工作目录 ...