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

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[稳定婚姻]的更多相关文章

  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. unity web项目发布服务器Data file is corrupt (not a Unity W

    楼上问题需要在iis 中配置MIME 加一个 .unity3d MIME类型:application/octet-stream http://www.cnblogs.com/123ing/p/3913 ...

  2. 判断一个字符串是否为合法IP

    输入任意一个字符串,判断是否为合法IP bool IsIPAddress(const char * str){ //先判断形式是否合法, //检查是否只包含点和数字 ; str[i] != '\0'; ...

  3. 04-1下载Win系统(装机助理)

    下载Win系统(装机助理): http://www.zhuangjizhuli.com/upan.html http://www.krlxx.com/64win7.html 选择你需要安装的系统: 以 ...

  4. CentOS6.5配置PHP CI程序

    步骤: 1.安装CentOS6.5系统:     1.选择PHP+Mysql环境 2.关闭防火墙和SeLinux     1.chkconfig --level 35 iptables off     ...

  5. 未能加载文件或程序集“WebGrease, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

    方法一:在web.config的configuration接点中添加,最好是添加在configuration节点的最后 <runtime> <assemblyBinding xmln ...

  6. NIO之Channel聚集(gather)写入与分散(scatter)读取

    Channel聚集(gather)写入 聚集写入( Gathering Writes)是指将多个 Buffer 中的数据“聚集”到 Channel. 特别注意:按照缓冲区的顺序,写入 position ...

  7. Visual Studio - 创建和使用动态库

    一.VS2013 创建动态库 1.1 新建项目 1.2.在Win32应用程序向导对话框上勾选“DLL”和“空项目”复选框,点完成 1.3 .添加对应的.C文件和.h文件 1.4 在.h文件中添加如下代 ...

  8. maven项目工程报错:cannot be resolved to a type

    1.在本地仓库中,搜索“_maven.repositories”所有匹配项,并彻底删除 2.然后再删除“.lastUpdated”所有匹配项 3.最后再重新在eclipse中执行操作:update d ...

  9. 【Mac + Appium学习(一)】之安装Appium环境前提准备

    环境: Appium version :1.9.1 Appium-desktop:1.7.1 Xcode:10.0 IOS:iPhone5S(10.3.3) Android:6.0.1 Mac:10. ...

  10. IDEA中maven打包跳过Junit Test

    运行MVN install时需要跳过Junit的test cases,可以采用下面的方法: mvn install -DskipTests 或者mvn install -Dmaven.test.ski ...