The Stable Marriage Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2974   Accepted: 1267

Description

The stable marriage problem consists of matching members of two different sets according to the member’s preferences for the other set’s members. The input for our problem consists of:

  • 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 (mf) 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

The first line gives you the number of tests. The first line of each test case contains integer n (0 < n < 27). Next line describes n male and n female names. Male name is a lowercase letter, female name is an upper-case letter. Then go n lines, that describe preferable lists for males. Next n lines describe preferable lists for females.

Output

For each test case find and print the pairs of the stable marriage, which is male-optimal. The pairs in each test case must be printed in lexicographical order of their male names as shown in sample output. Output an empty line between test cases.

Sample Input

  1. 2
  2. 3
  3. a b c A B C
  4. a:BAC
  5. b:BAC
  6. c:ACB
  7. A:acb
  8. B:bac
  9. C:cab
  10. 3
  11. a b c A B C
  12. a:ABC
  13. b:ABC
  14. c:BCA
  15. A:bac
  16. B:acb
  17. C:abc

Sample Output

  1. a A
  2. b B
  3. c C
  4.  
  5. a B
  6. b A
  7. c C

Source

题目大意:
    有N位女士和N位男士,每位男士或者女士都对对方有个评价。
他们会形成N对夫妻,如果A和a结婚,B和b结婚,但是A更偏爱b而非a而且b也更偏爱A而非B,那么这种婚姻是不稳定的 .
求 一个稳定的 婚姻 配对。

题目要求是男士优先,也就是男士优先表白,肯定会从最喜欢的开始表白,如果对方没有男友或者表白的男士优于当前男友,就会抛弃原来的男友,而接受表白的男士,男士也成功脱光。否则男士会被拒绝,便只能考虑下一个喜欢的女士。直到所有人都脱光,不存在拒绝情况为止。
采用 Gale-Shapley算法 :
    男生不停的求婚,女生不停地拒绝.
    算法保证:每一位男性得到的伴侣都是所有可能的稳定婚姻搭配方案中最理想的,同时每一位女性得到的伴侣都是所有可能的稳定婚姻搭配方案中最差的。

是不是只有唯一的稳定婚姻呢,觉得如果没有对两个人的评价完全相同的情况下,应该是唯一的.

 

  • Source Code

    1. #include<cstdio>
    2. #include<cstring>
    3. #include<queue>
    4. #include<algorithm>
    5. using namespace std;
    6. const int N=30;
    7. int malelike[N][N],femalelike[N][N];
    8. int malechoice[N],femalechoice[N];
    9. int malename[N],femalename[N];
    10. int T,couple;char str[N];
    11. queue<int>freemale;
    12. int main(){
    13. scanf("%d",&T);
    14. while(T--){
    15. while(!freemale.empty()) freemale.pop();
    16. scanf("%d",&couple);
    17. for(int i=0;i<couple;i++) scanf("%s",str),freemale.push(malename[i]=str[0]-'a');
    18. sort(malename,malename+couple);
    19. for(int i=0;i<couple;i++) scanf("%s",str),femalename[i]=str[0]-'A';
    20. for(int i=0;i<couple;i++){
    21. scanf("%s",str);
    22. for(int j=0;j<couple;j++){
    23. malelike[i][j]=str[j+2]-'A';
    24. }
    25. }
    26. for(int i=0;i<couple;i++){
    27. scanf("%s",str);
    28. for(int j=0;j<couple;j++){
    29. femalelike[i][str[j+2]-'a']=couple-j;
    30. }
    31. femalelike[i][couple]=0;
    32. }
    33. memset(malechoice,0,(couple+2)<<2);
    34. for(int i=0;i<couple;i++) femalechoice[i]=couple;
    35. while(!freemale.empty()){
    36. int male=freemale.front();
    37. int female=malelike[male][malechoice[male]];
    38. if(femalelike[female][male]>femalelike[female][femalechoice[female]]){
    39. freemale.pop();
    40. if(femalechoice[female]!=couple){
    41. freemale.push(femalechoice[female]);
    42. malechoice[femalechoice[female]]++;
    43. }
    44. femalechoice[female]=male;
    45. }
    46. else malechoice[male]++;
    47. }
    48. for(int i=0;i<couple;i++) printf("%c %c\n",malename[i]+'a',malelike[malename[i]][malechoice[malename[i]]]+'A');
    49. if(T) putchar('\n');
    50. }
    51. return 0;
    52. }
 
 

POJ3487[稳定婚姻]的更多相关文章

  1. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)

    The Stable Marriage Problem   Description The stable marriage problem consists of matching members o ...

  2. 图论补档——KM算法+稳定婚姻问题

    突然发现考前复习图论的时候直接把 KM 和 稳定婚姻 给跳了--emmm 结果现在刷训练指南就疯狂补档.QAQ. KM算法--二分图最大带权匹配 提出问题 (不严谨定义,理解即可) 二分图 定义:将点 ...

  3. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

  4. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  5. BZOJ2140: 稳定婚姻

    题解: 题意就是求二分图的必须边. 我们有结论: 在残量网络上跑tarjan,对于一条边(u,v) 如果该边满流||scc[u]==scc[v],那么该边是可行边. 因为如果scc[u]==scc[v ...

  6. 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)

    Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...

  7. 【稳定婚姻问题】【HDU1435】【Stable Match】

    2015/7/1 19:48 题意:给一个带权二分图  求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...

  8. poj 3487 稳定婚姻

    /** 稳定婚姻:男生不停的求婚,女生不停地拒绝 **/ #include <iostream> #include <queue> #include <cstdio> ...

  9. 稳定婚姻问题和Gale-Shapley算法(转)

    什么是算法?每当有人问作者这样的问题时,他总会引用这个例子:假如你是一个媒人,有若干个单身男子登门求助,还有同样多的单身女子也前来征婚.如果你已经知道这些女孩儿在每个男孩儿心目中的排名,以及男孩儿们在 ...

随机推荐

  1. iOS 系统框架

    iOS的系统架构分为四个层次:核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒体层(Media layer)和可触摸层(Cocoa Touch l ...

  2. Java中Vector与ArrayList的差别具体解释

    首先看这两类都实现List接口,而List接口一共同拥有三个实现类.各自是ArrayList.Vector和LinkedList.List用于存放多个元素,可以维护元素的次序,而且同意元素的反复. 3 ...

  3. 模板:什么是Traits

    Traits不是一种语法特性,而是一种模板编程技巧.Traits在C++标准库,尤其是STL中,有着不可替代的作用.   如何在编译期间区分类型   下面我们看一个实例,有四个类,Farm.Worke ...

  4. Linux如何根据UUID自动挂载磁盘分区

    一般服务器都有多个硬盘分区,在重启后,这些分区的逻辑位置加载时可能会发生变动,如果使用传统的设备名称(例如:/dev/sda)方式挂载磁盘,就可能因为磁盘顺序变化而造成混乱. Linux环境中每个Bl ...

  5. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  6. Eclipse 一直Building Workspace 的解决办法

    Eclipse 一直不停 building workspace完美解决总结 一.产生这个问题的原因多种 1.自动升级 2.未正确关闭  3.maven下载lib挂起 等.. 二.解决总结 (1).解决 ...

  7. sql server 循环

    declare @i int declare @a int declare @b int set @i=0 while(@i<=20) begin set @a=@i*1000+1; set @ ...

  8. rabbitmqctl 报错

    RabbitMQ 安装成功后,cmd dos命令进入RabbitMQ的安装路径,如 E:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.10\sb ...

  9. JAVASCRIPT中{} + {}的结果是什么?

    转自:http://www.heyria.com/index.php/2014/01/js-object-plus-object/ 当对象或者数组相加的时候,会产生有点意外的结果. 这篇文章主要是解释 ...

  10. jquery treegrid实例

    前台jqurey代码 function organDatagrid(){ $organ_treegrid = $('#organ_treegrid').treegrid({ url:ctx+'/pet ...