POJ3487[稳定婚姻]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2974 | Accepted: 1267 |
Description
- a set M of n males;
- a set F of n females;
- for each male and female we have a list of all the members of the opposite gender in order of preference (from the most preferable to the least).
A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (m, f) such that f ∈ F prefers m ∈ M to her current partner and m prefers f over his current partner. The stable marriage A is called male-optimal if there is no other stable marriage B, where any male matches a female he prefers more than the one assigned in A.
Given preferable lists of males and females, you must find the male-optimal stable marriage.
Input
Output
Sample Input
2
3
a b c A B C
a:BAC
b:BAC
c:ACB
A:acb
B:bac
C:cab
3
a b c A B C
a:ABC
b:ABC
c:BCA
A:bac
B:acb
C:abc
Sample Output
a A
b B
c C a B
b A
c C
Source
题目大意:
有N位女士和N位男士,每位男士或者女士都对对方有个评价。
他们会形成N对夫妻,如果A和a结婚,B和b结婚,但是A更偏爱b而非a而且b也更偏爱A而非B,那么这种婚姻是不稳定的 .
求 一个稳定的 婚姻 配对。
题目要求是男士优先,也就是男士优先表白,肯定会从最喜欢的开始表白,如果对方没有男友或者表白的男士优于当前男友,就会抛弃原来的男友,而接受表白的男士,男士也成功脱光。否则男士会被拒绝,便只能考虑下一个喜欢的女士。直到所有人都脱光,不存在拒绝情况为止。
采用 Gale-Shapley算法 :
男生不停的求婚,女生不停地拒绝.
算法保证:每一位男性得到的伴侣都是所有可能的稳定婚姻搭配方案中最理想的,同时每一位女性得到的伴侣都是所有可能的稳定婚姻搭配方案中最差的。
是不是只有唯一的稳定婚姻呢,觉得如果没有对两个人的评价完全相同的情况下,应该是唯一的.
- Source Code
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N=30;
int malelike[N][N],femalelike[N][N];
int malechoice[N],femalechoice[N];
int malename[N],femalename[N];
int T,couple;char str[N];
queue<int>freemale;
int main(){
scanf("%d",&T);
while(T--){
while(!freemale.empty()) freemale.pop();
scanf("%d",&couple);
for(int i=0;i<couple;i++) scanf("%s",str),freemale.push(malename[i]=str[0]-'a');
sort(malename,malename+couple);
for(int i=0;i<couple;i++) scanf("%s",str),femalename[i]=str[0]-'A';
for(int i=0;i<couple;i++){
scanf("%s",str);
for(int j=0;j<couple;j++){
malelike[i][j]=str[j+2]-'A';
}
}
for(int i=0;i<couple;i++){
scanf("%s",str);
for(int j=0;j<couple;j++){
femalelike[i][str[j+2]-'a']=couple-j;
}
femalelike[i][couple]=0;
}
memset(malechoice,0,(couple+2)<<2);
for(int i=0;i<couple;i++) femalechoice[i]=couple;
while(!freemale.empty()){
int male=freemale.front();
int female=malelike[male][malechoice[male]];
if(femalelike[female][male]>femalelike[female][femalechoice[female]]){
freemale.pop();
if(femalechoice[female]!=couple){
freemale.push(femalechoice[female]);
malechoice[femalechoice[female]]++;
}
femalechoice[female]=male;
}
else malechoice[male]++;
}
for(int i=0;i<couple;i++) printf("%c %c\n",malename[i]+'a',malelike[malename[i]][malechoice[malename[i]]]+'A');
if(T) putchar('\n');
}
return 0;
}
POJ3487[稳定婚姻]的更多相关文章
- 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage Problem Description The stable marriage problem consists of matching members o ...
- 图论补档——KM算法+稳定婚姻问题
突然发现考前复习图论的时候直接把 KM 和 稳定婚姻 给跳了--emmm 结果现在刷训练指南就疯狂补档.QAQ. KM算法--二分图最大带权匹配 提出问题 (不严谨定义,理解即可) 二分图 定义:将点 ...
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- BZOJ2140: 稳定婚姻
题解: 题意就是求二分图的必须边. 我们有结论: 在残量网络上跑tarjan,对于一条边(u,v) 如果该边满流||scc[u]==scc[v],那么该边是可行边. 因为如果scc[u]==scc[v ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- 【稳定婚姻问题】【HDU1435】【Stable Match】
2015/7/1 19:48 题意:给一个带权二分图 求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...
- poj 3487 稳定婚姻
/** 稳定婚姻:男生不停的求婚,女生不停地拒绝 **/ #include <iostream> #include <queue> #include <cstdio> ...
- 稳定婚姻问题和Gale-Shapley算法(转)
什么是算法?每当有人问作者这样的问题时,他总会引用这个例子:假如你是一个媒人,有若干个单身男子登门求助,还有同样多的单身女子也前来征婚.如果你已经知道这些女孩儿在每个男孩儿心目中的排名,以及男孩儿们在 ...
随机推荐
- JSON,字符串,MAP转换
package com.tree.autotest.testcase.IAuditBillDetailService; import com.alibaba.fastjson.JSON;import ...
- Mac电脑下配置maven环境变量
Mac电脑下配置maven环境变量 打开终端,使用 touch 命令创建 .bash_profile 文件 touch .bash_profile 编辑刚刚创建的文件 .bash_profile vi ...
- 解决 The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to have it working
事由: 迁移server的时候须要操作数据库.将数据库也进行迁移,在新server中导入数据的时候提示 The 'InnoDB' feature is disabled; you need MySQL ...
- HBase源代码分析之MemStore的flush发起时机、推断条件等详情(二)
在<HBase源代码分析之MemStore的flush发起时机.推断条件等详情>一文中,我们具体介绍了MemStore flush的发起时机.推断条件等详情.主要是两类操作.一是会引起Me ...
- IOS 应用程序路径
-- 获取资源文件目录 .app/下的图片,音频等资源文件 print("bundlePath" + NSBundle.mainBundle().bundlePath) //以下是 ...
- 预装WIN8改装WIN7之BIOS设置
不少预装WIN8/10的朋友觉得WIN8/10不好用,想改装WIN7,可改装之后常常出现各种问题,甚至不能启动,往往是BIOS设置不当. 本文以联想小新V2000 预装WIN8.1中文版为例,说说WI ...
- 通过 P3P规范让IE跨域接受第三方cookie session
所谓第三方 cookie,就是说你访问网页 A,却接收到域名 B 的 cookie 设定指令.这可能是由于网页 A 请求或链接了 B 的网页,比如上面提到的 iframe 以及 jsonp. 我查到了 ...
- formail 发送HTML 邮件通过 SENDMAIL
cat a.html | formail -I "Content-type:text/html;charset=utf-8" -I "Subject:layer4 con ...
- Android中*_handle_t/ANativeWindowBuffer/ANativeWindow/GraphicBuffer/Surface的关系
在阅读SurfaceFlinger HardwareComposer以及gralloc相关代码的过程中,我们经常会遇到native_handle private_handle_t ANativeWin ...
- 【转】锤子CTO钱晨:福利好是一种堕落的公司文化
“这是拉勾网对锤子科技CTO钱晨的访谈,作为中国手机界三大产品经理之一,他带领着一众硬件工程师在手机红海中厮杀.钱晨喜欢焦虑的工程师,佩服有方向感的人. 本文作者:西岳 拉勾网原创出品,转载请注明作者 ...