【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914
题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序,同时每位男生也按照自己的偏爱程度将女生排序。然后将这n个女生和n个男生配成完备婚姻。
如果存在两位女生A和B,两位男生a和b,使得A和a结婚,B和b结婚,但是A更偏爱b而不是a,b更偏爱A而不是B,则这个婚姻就是不稳定的,A和b可能背着别人相伴而走,因为他俩都认为,与当前配偶比起来他们更偏爱各自的新伴侣。
如果完备婚姻不是不稳定的,则称其是稳定的。通过证明,可以得到每一个n女n男的社团,都存在稳定婚姻的结论。但是这种情况只在异性的社团中存在。也就是说在同性的社团里面,稳定婚姻的存在性将不再被保证。
思路:先把所有男士加入队列当中,对于第一个出队列的男士从他喜爱度最高的女士开始求婚,如果找到一个女士还没有结婚,则和她匹配,如果找到一个女士,该女士对他的满意度高于这个女士的未婚夫,则该女士抛弃未婚夫和他进行匹配,她的未婚夫则进队列。已经匹配过的要进行标记,下次不能再匹配了。
因为每个男士最多和一个女士匹配一次。时间复杂度接近于O(n^2)。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include <queue>
#include <map>
using namespace std; const int maxn=;
int g[maxn][maxn], b[maxn][maxn], visit[maxn][maxn];
int bf[maxn], gf[maxn];
char ch[maxn], str[maxn];
map<char,int>G,M;
map<int,char>GG,MM;
queue<int>q;
int n, T; void init()
{
G.clear(), M.clear(), GG.clear(), MM.clear();
memset(visit,,sizeof(visit));
memset(bf,,sizeof(bf));
while(!q.empty()) q.pop();
} void find(int x)
{
for(int i=n; i>=; i--)
{
if(visit[x][i]) continue;
visit[x][i]=;
int y=b[x][i];
if(!bf[y])
{
bf[y]=x;
gf[x]=y;
return ;
}
else
{
if(g[y][x]>g[y][ bf[y] ])
{
q.push(bf[y]);
bf[y]=x;
gf[x]=y;
return ;
}
}
}
} void Solve()
{
for(int i=; i<=n; i++) q.push(i);
while(!q.empty())
{
int x=q.front();
q.pop();
find(x);
}
sort(ch+,ch+n+);
for(int i=; i<=n; i++)
printf("%c %c\n",ch[i],MM[ gf[ G[ch[i]] ] ]);
} int main()
{
cin >> T;
while(T--)
{
cin >> n;
init();
for(int i=; i<=n; i++) cin >> ch[i], G[ ch[i] ]=i, GG[i]=ch[i];
for(int i=; i<=n; i++) cin >> ch[n+i], M[ ch[n+i] ]=i, MM[i]=ch[n+i];
for(int i=; i<=n; i++)
{
scanf("%s",str+);
int x=G[ str[] ];
for(int j=; j<=n+; j++)
{
int y=M[ str[j] ];
b[x][n-j+]=y;
}
}
for(int i=; i<=n; i++)
{
scanf("%s",str+);
int x=M[ str[] ];
for(int j=; j<=n+; j++)
{
int y=G[ str[j] ];
g[x][y]=n-j+;
}
}
Solve();
if(T)puts("");
}
}
【HDU1914 The Stable Marriage Problem】稳定婚姻问题的更多相关文章
- 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage Problem Description The stable marriage problem consists of matching members o ...
- poj 3478 The Stable Marriage Problem 稳定婚姻问题
题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的. 所谓不稳婚姻是说.比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的 ...
- POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)
Description The stable marriage problem consists of matching members of two different sets according ...
- 【转】稳定婚姻问题(Stable Marriage Problem)
转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...
- The Stable Marriage Problem
经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...
- HDOJ 1914 The Stable Marriage Problem
rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 6553 ...
- [POJ 3487]The Stable Marriage Problem
Description The stable marriage problem consists of matching members of two different sets according ...
- 【HDOJ】1914 The Stable Marriage Problem
稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...
- hdoj1435 Stable Match(稳定婚姻问题)
简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...
随机推荐
- SQLServer两张表筛选相同数据和不同数据
概述 项目中经常会对两张数据库表的数据进行比较,选出相同的数据或者不同的数据.在SQL SERVER 2000中只能用Exists来判断,到了SQL SERVER 2005以后可以采用EXCEPT和I ...
- 在Activity中响应ListView内部按钮的点击事件
最近交流群里面有人问到一个问题:如何在Activity中响应ListView内部按钮的点击事件,不要在Adapter中响应? 对于这个问题,我最初给他的解答是,在Adapter中定义一个回调接口,在A ...
- Redis 的 5 个常见使用场景
2015-07-22 23:31:46 本文由 伯乐在线 - 刘晓鹏 翻译,黄利民 校稿.未经许可,禁止转载!英文出处:Joe Engel.欢迎加入翻译组. 在这篇文章中,我们将阐述 Redis 最常 ...
- Angular.js 以及个人学习网站
Angular.js 教程 http://www.360doc.com/content/14/0414/15/14416931_368816305.shtml web前端学习: 慕课网:http:/ ...
- Js+XML 操作
xml文件Login.xml如下. 复制代码 代码如下: <?xml version="1.0" encoding="utf-8" ?> <L ...
- 安装配置hive中遇到的问题
1. mysql中添加用户名时总出现如下问题:ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables ...
- oracle的内置函数
1.wmsys.wm_concat 行转列函数 select wmsys.wm_concat(destnumber) from mms_send_his_record group by sendn ...
- iOS学习06C语言结构体
1.结构体的概述 在C语言中,结构体(struct)指的是一种数据结构,是C语言中构造类型的其中之一. 在实际应用中,我们通常需要由不同类型的数据来构成一个整体,比如学生这个整体可以由姓名.年龄.身高 ...
- POJ 1845 (约数和+二分等比数列求和)
题目链接: http://poj.org/problem?id=1845 题目大意:A^B的所有约数和,mod 9901. 解题思路: ①整数唯一分解定理: 一个整数A一定能被分成:A=(P1^K1) ...
- 使用javamail发信过程中的一些问题及解决方法
http://www.blogjava.net/TrampEagle/archive/2006/05/26/48326.html 今天在研究javamail发信的过程中,出现了一些小问题,现总结如下, ...