HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
Bipartite Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 840 Accepted Submission(s): 285
Note: There must be at most one edge between any pair of vertices both in the new graph and old graph.
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.
// 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优化】的更多相关文章
- HDU 5313 Bipartite Graph
题意:给一个二分图,问想让二分图变成完全二分图最多能加多少条边. 解法:图染色+dp+bitset优化.设最终的完全二分图两部分点集为A和B,A中点个数为x,B中点个数为y,边数则为x × y,答案即 ...
- HDU 5313 Bipartite Graph (二分图着色,dp)
题意: Soda有一个n个点m条边的二分图, 他想要通过加边使得这张图变成一个边数最多的完全二分图. 于是他想要知道他最多能够新加多少条边. 注意重边是不允许的. 思路: 先将二分图着色,将每个连通分 ...
- 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(dfs染色 或者 并查集)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- HDU 5890 Eighty seven(DP+bitset优化)
题目链接 Eighty seven 背包(用bitset预处理)然后对于每个询问O(1)回答即可. 预处理的时候背包. #include <bits/stdc++.h> using nam ...
- hdu 5745 La Vie en rose DP + bitset优化
http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...
- hdu5745 La Vie en rose 巧妙地dp+bitset优化+滚动数组减少内存
/** 题目:hdu5745 La Vie en rose 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5745 题意:题目给出的变换规则其实就是交换相邻 ...
- 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] = ...
- HDU5745-La Vie en rose-字符串dp+bitset优化
这题现场的数据出水了,暴力就能搞过. 标解是拿bitset做,转移的时候用bitset优化过的操作(与或非移位)来搞,复杂度O(N*M/w) w是字长 第一份标程的思路很清晰,然而后来会T. /*-- ...
随机推荐
- .NET架构转Java开发必须了解的历史
终于不在职守在.NET领域 .NET的winform和webform项目也开发了很多了 尤其是WEB领域 从ASP -> ASP.NET 2.0 -> ASP.NET MVC 4.0 - ...
- 《Servlet和jsp学习指南》 笔记1
chapter 1 Servlet 4个java 包: 对于每一个http请求,Servlet请求都会创建一个ServletRequest实例,并将它传给Servlet的service方法.Servl ...
- Codeforces Round #546 (Div. 2)D(贪心,思维,SET,VECTOR,模拟)
#include<bits/stdc++.h>using namespace std;int a[300007],b[500007],c[500007];set<int>st[ ...
- 泛型1(一些algorithm函数)
泛型算法本身不会执行容器的操作,它们只会运行于迭代器之上,执行迭代器的操作.因此算法可能改变容器中保存的元素,也可能在容器内移动元素,但永远不会直接添加或删除元素. 只读算法: accumulate: ...
- SpringMVC中web.xml的配置(直接coppy使用就行)
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...
- 2、Tensorflow中的变量
2.Tensorflow中的变量注意:tf中使用 变量必须先初始化下面是一个使用变量的TF代码(含注释): # __author__ = "WSX" import tensorfl ...
- 老男孩Day13作业:ORM学员管理系统
一.作业需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图: 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上 ...
- 4A - Horse
打表找规律 #include <iostream> #include <queue> using namespace std; ][]; ]{, , , , -, -, -, ...
- 洛谷 P4001 [ICPC-Beijing 2006]狼抓兔子
题目描述 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: ...
- Qt 学习之路 2(54):剪贴板
Qt 学习之路 2(54):剪贴板 豆子 2013年6月8日 Qt 学习之路 2 2条评论 剪贴板的操作经常和前面所说的拖放技术在一起使用.大家对剪贴板都很熟悉.我们可以简单地把它理解成一个数据存储池 ...