Bipartite Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 840    Accepted Submission(s): 285

Problem Description
Soda has a bipartite graph with n vertices and m undirected edges. Now he wants to make the graph become a complete bipartite graph with most edges by adding some extra edges. Soda needs you to tell him the maximum number of edges he can add.

Note: There must be at most one edge between any pair of vertices both in the new graph and old graph.

 
Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤100), indicating the number of test cases. For each test case:

The first line contains two integers n and m, (2≤n≤10000,0≤m≤100000).

Each of the next m lines contains two integer u,v (1≤u,v≤n,v≠u) which means there's an undirected edge between vertex u and vertex v.

There's at most one edge between any pair of vertices. Most test cases are small.

 
Output
For each test case, output the maximum number of edges Soda can add.
 
Sample Input
2
4 2
1 2
2 3
4 4
1 2
1 4
2 3
3 4
 
Sample Output
2
0
 
 
题目大意:跟你一个二分图。n个顶点,m条边。问你再添加多少条边,让这个二分图变成完全二分图。
 
 
解题思路:为了让加的边尽量多,那么就要保证该二分图两侧的顶点个数尽量相同。那么我们统计出各个二分图连通块两边各有多少个顶点,最后用dp来得出两边相差最少时,两边会有多少个顶点。最后套个公式就可以了。本来用二维dp01背包。但是超时。然后就换用了bitset进行优化。
 
 
// bitset::reset
#include <iostream> // std::cout
#include <string> // std::string
#include <bitset> // std::bitset int main ()
{
std::bitset<4> foo (std::string("1011")); std::cout << foo.reset(1) << '\n'; // 1001
std::cout << foo.reset() << '\n'; // 0000 return 0;
} // bitset::operator[]
#include <iostream> // std::cout
#include <bitset> // std::bitset int main ()
{
std::bitset<4> foo; foo[1]=1; // 0010
foo[2]=foo[1]; // 0110 std::cout << "foo: " << foo << '\n'; return 0;
}

  

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<bitset>
#include<vector>
using namespace std;
const int maxn=1e4+20;
#define min(a,b) ((a)<(b)?(a):(b))
vector<int>G[maxn];
int color[maxn],d[maxn][3];
int n,m;
void dfs(int cc,int u,int col){
// printf("%d---%d----\n",u,cc);
if(!color[u]){
color[u]=col;
d[cc][col+1]++;
}
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(!color[v]){
dfs(cc,v,-col);
}
}
}
int main(){
int t,a,b;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
int c=0;
for(int i=1;i<=n;i++){
if(!color[i]){
c++;
dfs(c,i,1);
}
}
const int V=5001;
bitset<V>dp;
dp.reset();
dp[0]=1;
for(int i=1;i<=c;i++){
dp=(dp<<d[i][0])|(dp<<d[i][2]);
}
int k=1;
for(int i=n/2;i>=1;i--){
if(dp[i]){
k=i;
break;
}
}
printf("%d\n",(n-k)*k-m);
for(int i=0;i<=n;i++)
G[i].clear();
memset(color,0,sizeof(color));
memset(d,0,sizeof(d));
}
return 0;
} /*
8
8 6
1 2
2 3
2 4
5 6
6 7
6 8
*/

  

 
 
 

HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】的更多相关文章

  1. HDU 5313 Bipartite Graph

    题意:给一个二分图,问想让二分图变成完全二分图最多能加多少条边. 解法:图染色+dp+bitset优化.设最终的完全二分图两部分点集为A和B,A中点个数为x,B中点个数为y,边数则为x × y,答案即 ...

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

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

  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(dfs染色 或者 并查集)

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

  5. HDU 5890 Eighty seven(DP+bitset优化)

    题目链接 Eighty seven 背包(用bitset预处理)然后对于每个询问O(1)回答即可. 预处理的时候背包. #include <bits/stdc++.h> using nam ...

  6. hdu 5745 La Vie en rose DP + bitset优化

    http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...

  7. hdu5745 La Vie en rose 巧妙地dp+bitset优化+滚动数组减少内存

    /** 题目:hdu5745 La Vie en rose 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5745 题意:题目给出的变换规则其实就是交换相邻 ...

  8. HDU 5808 Price List Strike Back bitset优化的背包。。水过去了

    http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...

  9. HDU5745-La Vie en rose-字符串dp+bitset优化

    这题现场的数据出水了,暴力就能搞过. 标解是拿bitset做,转移的时候用bitset优化过的操作(与或非移位)来搞,复杂度O(N*M/w) w是字长 第一份标程的思路很清晰,然而后来会T. /*-- ...

随机推荐

  1. 警惕C#事件使用过程中的GC陷阱

    关于C#中的事件,园里已经有大量的文章对其内在实现做过剖析,如果还不甚了解的可以阅读这篇文章 通过Demo来细看C#事件的内在机制 虽然比较早,但非常清楚地展示了事件的内部机制,总结一下就是 1.事件 ...

  2. EF 热加载 Winform/Asp.net

    public partial class Form1 : Form { BackgroundWorker worker = new BackgroundWorker(); xxContext cont ...

  3. 使用 typescript ,提升 vue 项目的开发体验(1)

    此文已由作者张汉锐授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言:对于我们而言,typescript 更像一个工具 官方指南 从 vue2.5 之后,vue 对 ts ...

  4. 《spring 攻略》笔记1

    chapter1 spring简介 两种spring ioc容器实现类型: BeanFactory ApplicationContext 应用程序上下文 DI技巧: @Autowired(requir ...

  5. time,datetime,calendar模块

    Python中,与时间有关的模块有time,datetime和calendar. 1.时钟时间:time 在Python中,用三种方式来表示时间:时间戳,格式化时间字符串和结构化时间. 1)时间戳,就 ...

  6. A - 活动安排问题(贪心)

    A - 活动安排问题 有若干个活动,第i个开始时间和结束时间是[Si,fi),同一个教室安排的活动之间不能交叠,求要安排所有活动,最少需要几个教室?  Input第一行一个正整数n (n <= ...

  7. 性能测试常用sql语句

    做了一段时间的性能测试,把自己在性能测试过程中,使用到的Oracle中用到的sql语句整理一番,做个备忘: (1)多个字段以某种格式拼接 Oracle方式:"||"字符串拼接符; ...

  8. jmeter 正则表达式的关联

    在工作中,用JM录制了登录---退出的脚本,但是多次请求后发现,总是返回的录制脚本的时候使用的账号的数据. 经过研究发现,login后,响应里的每次返回的token值是变化的,顺着往下看,下一个请求中 ...

  9. ThinkCMF Foreach标签

    foreach标签类似与volist标签,只是更加简单,没有太多额外的属性,例如: <foreach name="list" item="vo"> ...

  10. ARKit的使用

    //创建场景 let scene = SCNScene() /* //1.几何 let box = SCNBox.init(width: 0.1, height: 0.1, length: 0.1, ...