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. SQL Server乱码处理(ASCII)

    CREATE FUNCTION [dbo].[RegexReplace] ( @string VARCHAR(MAX), --被替换的字符串 @pattern VARCHAR(255), --替换模板 ...

  2. 家用wifi信号覆盖增强扩展实用指南

    家用wifi信号覆盖增强扩展实用指南 现在网上很多号称穿墙王的无线路由器,但是一般用起来效果都不理想,其实最主要的原因还是家里面一般每个房间不大,但是墙比较多.并且一般也没有一个所谓的中心点放置路由器 ...

  3. 「JSOI2009」球队收益 / 球队预算

    题目链接 戳我 \(Solution\) 我们发现这道题目并不好做,因为要考虑两个因素对答案的影响.于是我们假设接下来的\(m\)场比赛双方都输了.这要我们就只要考虑赢一场对答案的影响了,那每赢一场输 ...

  4. 一些c++多线程习题

    题目1:子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码 代码1: #include <iostr ...

  5. 【ARC074F】Lotus Leaves 最小割

    Description 给你一个n*m网格图,有起点荷叶和终点荷叶,有中转荷叶,其他的格子没东西,一个荷叶可以跳到同一行或者列的另一个荷叶.问最多删掉几个中转荷叶能让起点终点不连通.如果不行输出-1. ...

  6. Ubuntu16.04搭建各种开发环境的IDE: QT5 , CodeBlocks ,eclipse-cdt, PyCharm

    搭建Ubuntu下C/C++以及Python的集成开发环境,采用双系统(Win7+Ubuntu)的Ubuntu16.04-LTS系统, 关于双系统的搭建可以参考下面博客(图文十分详细):https:/ ...

  7. application的使用(实现计数器)

    application在整个WEB项目中共享使用数据. 常用方法: getAttribute(); setAttribute();示列: <%    Object count=applicati ...

  8. ubuntu15.04下安装docker

    ​##获得更多资料欢迎进入我的网站或者 csdn或者博客园 最近听说docker很火,不知道什么东西,只知道是一个容器,可以跨平台.闲来无事,我也来倒弄倒弄.本文主要介绍:ubuntu下的安装,以及基 ...

  9. 没有上司的舞会 树形dp

    题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri, ...

  10. day13--------python 内置函数(一)

    一:内置函数 内置函数就是python直接提供可以用的 01:作用域相关: locals() 返回当前作用域中的名字 globals() 返回全局作用域中的名字 02:迭代器相关: range() 生 ...