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

解法:图染色+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. easyui使用时出现这个Uncaught TypeError: Cannot read property 'combo' of undefined

    easyui使用时出现这个Uncaught TypeError: Cannot read property 'nodeName' of undefined 最后检查发现是必须给select一个id,光 ...

  2. 【学习总结】OS X , IOS , IOS SDK , XCode之间的关系

    几个基本的概念 : OS X : 属于桌面PC级别(IMac,MacPro等)对应安装的操作系统 IOS : 属于移动设备级别(Iphone,Ipad等)对应安装的操作系统 XCode: 是一个IDE ...

  3. iOS基本网络请求

    常见的网络请求有同步GET, 同步POST, 异步GET, 异步POST. GET请求和POST请求的区别: 1. GET请求的接口会包含参数部分,参数会作为网址的一部分,服务器地址与参数之间通过 ? ...

  4. HTTP幂等性

    http://www.cnblogs.com/weidagang2046/archive/2011/06/04/2063696.html 理解HTTP幂等性 基于HTTP协议的Web API是时下最为 ...

  5. 1064: [Noi2008]假面舞会 - BZOJ

    Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办 ...

  6. select框宽度与高度设置(实用版)

    在IE中只能使用 font-size: 限制 select 的高度.   同时使用 width:200px 限制宽度   size="20" 表示最多显示20个选项,超过20的需要 ...

  7. 如何使用PHP实现一个WebService

    WSDL WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问.这种文 ...

  8. hdu 1272

    并查集  要判断这个图是连通的 就是只有一个父节点 #include <cstdio> #include <cstring> #define maxn 100005 int f ...

  9. c++ ANSI、UNICODE、UTF8互转

        static std::wstring MBytesToWString(const char* lpcszString);    static std::string WStringToMBy ...

  10. ZOJ 3778 Talented Chef

    题目链接 题意 : 这个人需要做n道菜,每道菜Ai步,他可以同时做M道不同的菜的其中一步,问你最少需要多少时间能做完所有的菜. 思路 : 这个题比赛的时候禁锢思路了,根本没想出来,就是当M > ...