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. 将以太坊封装为 ERC20

    将以太坊封装为 ERC20 TOKEN 很多 DAPP 都是在处理 ERC20接口的 token, 其实很容易将以太坊封装为 ERC20,这样就可以统一处理, 至少我目前在做的雷电网络就是这么处理的. ...

  2. Android TV 开发 (1)

    本文来自网易云社区 作者:孙有军 前言 这里主要记录几个TV问题的解决方案,如果对这个不感兴趣的其实就不用往下看了. 这几天有一个需求就是要求出一个TV版本的app,之前没有具体的了解Tv版的app有 ...

  3. day01.2-python基础

    一. python基本数据类型及操作     1. 数字.在python中,数字的初始化方式为直接赋值.如:a = 11 a). 加法运算                              b ...

  4. April Fools Day Contest 2019 A. Thanos Sort

    A. Thanos Sort time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. 题解 P1339 【[USACO09OCT]热浪Heat Wave】

    题目链接 这道题纯属是一个裸的SPFA: 建议先把模板AC之后再做. 只需要做一些手脚,就是在加边的时候加一个双向边就好. 然后再第一次加点的时候 看不懂模板的出门左转度娘. 推荐下面一片讲解: 友链 ...

  6. loj #2508. 「AHOI / HNOI2018」游戏

    #2508. 「AHOI / HNOI2018」游戏 题目描述 一次小 G 和小 H 在玩寻宝游戏,有 nnn 个房间排成一列,编号为 1,2,…,n,相邻房间之间都有 111 道门.其中一部分门上有 ...

  7. SDUT OJ 数据结构实验之图论四:迷宫探索

    数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  8. 品味ZooKeeper之Watcher机制_2

    品味ZooKeeper之Watcher机制 本文思维导图如下: 前言 Watcher机制是zookeeper最重要三大特性数据节点Znode+Watcher机制+ACL权限控制中的其中一个,它是zk很 ...

  9. Android 多个界面(Activity)

    1.介绍 2.相关属性 (1)启动Activity (2)Intent介绍 (3)关闭Activity 3.多个页面之间传递数据(页面1向页面2传递数据,单向传递数据) (1)相关属性 注意:data ...

  10. C++_代码重用5-类模板

    如果两种类只是数据类型不同,而其他代码是相同的,与其编写新的类声明,不如编写一种泛型(独立于类型的)栈.然后将具体的类型作为参数传递给这个类.这样就可以使用通用的代码生成存储不同类型值的栈. 可以使用 ...