HDU 5313 Bipartite Graph
题意:给一个二分图,问想让二分图变成完全二分图最多能加多少条边。
解法:图染色+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的更多相关文章
- hdu 5313 Bipartite Graph(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
Bipartite Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5313 Bipartite Graph(二分图染色+01背包水过)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- HDU 5313 Bipartite Graph (二分图着色,dp)
题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...
- 2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:h ...
- 二分图点染色 BestCoder 1st Anniversary($) 1004 Bipartite Graph
题目传送门 /* 二分图点染色:这题就是将点分成两个集合就可以了,点染色用dfs做, 剩下的点放到点少的集合里去 官方解答:首先二分图可以分成两类点X和Y, 完全二分图的边数就是|X|*|Y|.我们的 ...
- HDU 6321 Dynamic Graph Matching
HDU 6321 Dynamic Graph Matching (状压DP) Problem C. Dynamic Graph Matching Time Limit: 8000/4000 MS (J ...
- 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 ...
- CodeForces - 600F Edge coloring of bipartite graph
Discription You are given an undirected bipartite graph without multiple edges. You should paint the ...
随机推荐
- IOS9中出现的错误
1,Bitcode 错误提示: ld: '/Applications/Cocos/frameworks/cocos2d-x-3.8.1/prebuilt/ios/libcocos2d iOS.a(CC ...
- ios App 打包
ios 版本的 App 打包两种方式: 1. 命令行 xcodebuild exportArchive -exportFormat ipa 2. 通过 xcode Product -> Arch ...
- java第二课:运算符和表达式
1.取模%,如果余数为零,则判断可以整除.2.余数永远小于除数.3.自增运算符++或自减运算符--单独使用时,前++.--后++.--效果是一样的4.先加一,后使用,前++:先使用,后加一,后++5. ...
- c++ string char* const char*
#include <iostream> #include <string> #include <cstring> using namespace std; int ...
- CSS 命名规范及标题供参考与学习
一.CSS 命名规范 XHTML-CSS写作建议 所有的xhtml代码小写 属性的值一定要用双引号("")括起来,且一定要有值 每个标签都要有开始和结束,且要有正确的层次 空元 ...
- Good Bye 2015 A
Problem A:http://codeforces.com/problemset/problem/611/A A. New Year and Days 题意:某人要在2016年收集糖果,有两种不同 ...
- uva 701
参考了一下http://hi.baidu.com/renxl51/item/e80b688f9f54aadd5e0ec1de 给一个数字x,求最小的正整数e,使得pow(2,e) == x*pow(1 ...
- iOS设计模式——委托(delegate)
委托(delegate)也叫代理是iOS开发中常用的设计模式.我们借助于protocol(参考博文:objective-c协议(protocol))可以很方便的实现这种设计模式. 什么是代理? 苹果的 ...
- HDU 1429 胜利大逃亡(续)(三维BFS)
题目链接 题意 : 中文题不详述. 思路 : 这个题和1885差不多一样的,所以我直接改了改那个代码就交上了,链接 #include <stdio.h> #include <stri ...
- RCC 2014 Warmup (Div. 2) ABC
题目链接 A. Elimination time limit per test:1 secondmemory limit per test:256 megabytesinput:standard in ...