题目传送门

 /*
  二分图点染色:这题就是将点分成两个集合就可以了,点染色用dfs做, 剩下的点放到点少的集合里去
官方解答:首先二分图可以分成两类点X和Y, 完全二分图的边数就是|X|*|Y|.我们的目的是max{|X|*|Y|}, 并且|X|+|Y|=n.
修正:实现多连通块染色,然后贪心选择,将两个集合个数差大的连通块优先添加,能尽量使得un*vn最大
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std; const int MAXN = 1e4 + ;
const int MAXM = 1e5 + ;
const int INF = 0x3f3f3f3f;
vector<int> G[MAXN];
int col[*MAXN];
bool vis[MAXN];
struct Block { //连通块
int u, v;
bool operator < (const Block &r) const {
return u - v > r.u - r.v;
}
}b[MAXN];
int n, m, un, vn; void DFS(int u, int c) {
col[c]++; vis[u] = true;
for (int i=; i<G[u].size (); ++i) {
int v = G[u][i];
if (vis[v]) continue;
DFS (v, c ^ );
}
} int main(void) { //BestCoder 1st Anniversary($) 1004 Bipartite Graph
//freopen ("D.in", "r", stdin); int T; scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
for (int i=; i<=n; ++i) G[i].clear ();
for (int i=; i<=m; ++i) {
int u, v; scanf ("%d%d", &u, &v);
G[u].push_back (v); G[v].push_back (u);
} int color = ;
memset (vis, false, sizeof (vis));
memset (col, , sizeof (col));
for (int i=; i<=n; ++i) {
if (vis[i]) continue;
DFS (i, color); color += ;
}
int cnt = ;
for (int i=; i<color; i+=) {
b[++cnt].u = col[i]; b[cnt].v = col[i^];
if (b[cnt].u < b[cnt].v) swap (b[cnt].u, b[cnt].v);
}
sort (b+, b++cnt); un = vn = ;
for (int i=; i<=cnt; ++i) {
if (un <= vn) {
un += b[i].u; vn += b[i].v;
}
else {
un += b[i].v; vn += b[i].u;
}
}
printf ("%d\n", un * vn - m);
} return ;
}

二分图点染色 BestCoder 1st Anniversary($) 1004 Bipartite Graph的更多相关文章

  1. BestCoder 1st Anniversary 1004 Bipartite Graph 【二分图 + bfs + 良好的逻辑思维 】

    题目地址:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=610&pid=1004 问题描述 Soda有一个$ ...

  2. BestCoder 1st Anniversary

    Souvenir  Accepts: 1078  Submissions: 2366  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 26 ...

  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. BestCoder 1st Anniversary($) 1003 Sequence

    题目传送门 /* 官方题解: 这个题看上去是一个贪心, 但是这个贪心显然是错的. 事实上这道题目很简单, 先判断1个是否可以, 然后判断2个是否可以. 之后找到最小的k(k>2), 使得(m-k ...

  6. hdu 5311 Hidden String (BestCoder 1st Anniversary ($))(深搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=5311 Hidden String Time Limit: 2000/1000 MS (Java/Others)  ...

  7. BestCoder 1st Anniversary ——HDU5312(数学推导)

    Today, Soda has learned a sequence whose n-th (n≥1) item is 3n(n−1)+1. Now he wants to know if an in ...

  8. HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】

    Bipartite Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  9. [HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP)

    [HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP) 题面 有三个人从一张N个点无重边的有向无环图上的三个点出发,每单位时间,他们分别选择当前 ...

随机推荐

  1. oc中对象的初始化

    在.m文件中使用对象方法: - (id)init { _name =@"zhangsan"; _age = 18; return self; } 然后通过main方法中进行创建对象 ...

  2. 【分享】生成带数据的Insert语句工具(源码)

    这个工具用于导出带数据的Insert语句,方便在不同版本的数据库上导出数据.有人说sql server 2008自带导出带数据的Insert语句(连接)但是我的数据库版本没有这个选项,无奈之下自己简单 ...

  3. params关键字

    每个C#函数都允许有个参数带params关键字,在调用的时候可以不给他传值,也可以给他传值,还可以给他传多个值 注意事项: ·一个函数中只能一个参数带params关键字:·带params关键字的参数必 ...

  4. Qt---在QLabel上实现系统时间

    参考:http://blog.csdn.net/g457499940/article/details/11923887 ---------------------------------------- ...

  5. Python实现NN(神经网络)

    Python实现NN(神经网络) 参考自Github开源代码:https://github.com/dennybritz/nn-from-scratch 运行环境 Pyhton3 numpy(科学计算 ...

  6. 一、换系统wince ---到 linux ubuntu 桌面

    今天终于拿到了OK6410,还是比较满意.预装的为wince系统,现要修改为linux 按照  OK6410-A开发板LINUX3.0.1-2013-01用户手册 进行 1.遇到问题.无法写入SD卡( ...

  7. [原]项目进阶 之 集群环境搭建(二)MySQL集群

    上次的博文中我们介绍了一下集群的相关概念,今天的博文我们介绍一下MySQL集群的相关内容. 1.MySQL集群简介 MySQL群集技术在分布式系统中为MySQL数据提供了冗余特性,增强了安全性,使得单 ...

  8. 模仿cocos2dx 风格用工厂方法,实现class A,不使用宏,

    class A { static A *create(); bool init(); }; A* A::create() { A *pRet=new A; if(pRet && pRe ...

  9. 安装Windows7出现:”安装程序无法创建新的系统分区 也无法定位系统分区“ 终极解决方案

    参考:地址 解决方法: 1.先格式化一下你要装的那个盘,然后,拔出U盘,啥也别动,只拔出U盘就行,再装上U盘,然后刷新一下[选硬盘那里的高级选项中有格式化和刷新],再选择要安装的硬盘点下一步,OK了, ...

  10. Readhat Linux5.5 安装SVNService(经验总结)

    Subversion独立服务和与apache整合服务. 一 .Svn独立服务安装 操作系统: Redhat Linux5.5 安装包获取: 下载 http://subversion.tigris.or ...