nyoj 239 月老的难题
2016-05-07 10:44
267 查看
月老的难题
时间限制:1000 ms | 内存限制:65535 KB难度:4
描述
月老准备给n个女孩与n个男孩牵红线,成就一对对美好的姻缘。
现在,由于一些原因,部分男孩与女孩可能结成幸福的一家,部分可能不会结成幸福的家庭。
现在已知哪些男孩与哪些女孩如果结婚的话,可以结成幸福的家庭,月老准备促成尽可能多的幸福家庭,请你帮他找出最多可能促成的幸福家庭数量吧。
假设男孩们分别编号为1~n,女孩们也分别编号为1~n。
输入第一行是一个整数T,表示测试数据的组数(1<=T<=400)
每组测试数据的第一行有两个整数n,K,其中男孩的人数与女孩的人数都是n。(n<=500,K<=10 000)
随后的K行,每行有两个整数i,j表示第i个男孩与第j个女孩有可能结成幸福的家庭。(1<=i,j<=n)
输出对每组测试数据,输出最多可能促成的幸福家庭数量
样例输入
1 3 4 1 1 1 3 2 2 3 2
样例输出
2
匈牙利算法求二分图的最大匹配数,万幸发现了http://blog.csdn.net/dark_scope/article/details/8880547,然后算法才变得不那么无聊了。。。
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> using namespace std; const int maxn = 555; vector <int> g[maxn]; int T, n, k; int from[maxn], tot; bool use[maxn]; bool match(int x) { for(unsigned i = 0; i < g[x].size(); i++) { if(!use[g[x][i]]) { use[g[x][i]] = true; if(from[g[x][i]] == -1 || match(from[g[x][i]])) { from[g[x][i]] = x; return true; } } } return false; } int hungary() { tot = 0; memset(from, 255, sizeof(from)); for(int i = 1; i <= n; i++) { memset(use, false, sizeof(use)); //为什么每次都要清空有点理解无能 if(match(i)) tot++; } return tot; } int main() { scanf("%d", &T); while(T--) { scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++) { g[i].clear(); }//清空vector int boy, girl; while(k--) { scanf("%d%d", &boy, &girl); g[boy].push_back(girl); } printf("%d\n", hungary()); } return 0; }因为有多组数据却忘了每次要清空vector, 然后各种wa /(ㄒoㄒ)/~~
相关文章推荐
- 深度学习入门级框架MatConvNet环境配置(Ubuntu14.04+Cuda7.5+Cudnn5+Matlab2014a)
- 接口与抽象类的总结
- Geoserver的ImageMosaic数据源添加以及服务发布
- Lantern仅代理浏览器(不设置系统代理)
- Geoserver的ImageMosaic数据源添加以及服务发布
- 使用cocoaPods加载框架的具体步骤:
- poj 3177 Redundant Paths (双联通)
- ASP.NET MVC 表单验证方式总结
- zabbix网页安装过程中mysql的sock报错
- hdu 5678 ztr loves trees (给一颗有根树,树上的每一个节点有一个权值,每次询问某个子树中所有权值的中位数)
- Android 系统移植与调试(一)Android编译环境编译服务器搭建
- HDU 4734 F(x)
- 在ASP.NET 2.0中操作数据之十九:给编辑和新增界面增加验证控件
- 获取statusbar的高度
- C++位运算
- 数据库密码过期,账号被锁解决方法
- debian下载、编译、使用Cimg的Demo
- hihoCoder 1301 筑地市场 数位dp加二分答案
- Tomcat启动分析(我们为什么要配置CATALINA_HOME环境变量)
- 飛飛(三十一)指向学生的指针