The Stable Marriage Problem

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 758    Accepted Submission(s): 389

Problem 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 (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
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
 题意:裸婚姻匹配,注意男士排序时的序号一定不要弄混了。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=;
int pref[maxn][maxn],ord[maxn][maxn],fum[maxn],fuw[maxn],nex[maxn];
int t,n;
char ch[],s[],x;
int nu[];
map<char,int>vm,vw;
map<int,char>um,uw;
bool cmp(int x,int y)
{
return ch[x]<ch[y];
}
queue<int>q;
void engage(int m,int w)
{
int pm=fum[w];
if(pm){
fuw[pm]=;
q.push(pm);
}
fuw[m]=w;
fum[w]=m;
}
int main()
{
scanf("%d",&t);
while(t--){
memset(pref,,sizeof(pref));
vm.clear();vw.clear();um.clear();uw.clear();
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>ch[i];
nu[i]=i;
}
sort(nu+,nu++n,cmp);
for(int i=;i<=n;i++){
int tmp=nu[i];
vm[ch[tmp]]=tmp;
um[tmp]=ch[tmp];
fuw[tmp]=;
nex[tmp]=;
q.push(tmp);
}
for(int i=;i<=n;i++){
cin>>x;
vw[x]=i;
uw[i]=x;
fum[i]=;
}
for(int i=;i<=n;i++){
scanf("%s",s);
int tmp=vm[s[]];
for(int j=;j<n+;j++){
pref[tmp][j-]=vw[s[j]];
}
}
for(int i=;i<=n;i++){
scanf("%s",s);
int tmp=vw[s[]];
for(int j=;j<n+;j++){
ord[tmp][vm[s[j]]]=j-;
}
}
while(!q.empty()){
int m=q.front();
q.pop();
int w=pref[m][nex[m]++];
if(w==) continue;
if(!fum[w])
engage(m,w);
else if(ord[w][m]<ord[w][fum[w]])
engage(m,w);
else q.push(m);
}
while(!q.empty()) q.pop();
for(int i=;i<=n;i++){
int tmp=nu[i];
printf("%c %c\n",um[tmp],uw[fuw[tmp]]);
}
if(t!=) printf("\n");
}
return ;
}

HDU1914 稳定婚姻匹配的更多相关文章

  1. HDU1522 稳定婚姻匹配 模板

    Marriage is Stable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)

    <题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的 ...

  3. hdu1914 稳定婚姻问题

               稳定婚姻问题就是给你n个男的,n个女的,然后给你每个男生中女生的排名,和女生心目中男生的排名,然后让你匹配成n对,使婚姻稳定,假如a和b匹配,c和d匹配,如果a认为d比b好,同时 ...

  4. HDU 1522 Marriage is Stable 稳定婚姻匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1522 #include<bits/stdc++.h> #define INF 0x3f3f3f3f ...

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

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

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

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

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

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

  8. UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)

    题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...

  9. BZOJ2140: 稳定婚姻

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

随机推荐

  1. HTMLTestRunner解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)

    其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误. ...

  2. CP文件覆盖问题

    # \cp -r -a aaa/* /bbb[这次是完美的,没有提示按Y.传递了目录属性.没有略过目录]

  3. Java学习个人备忘录之文档注释

    文档注释 单行注释用 // 多行注释有两种,第一种是 /* 内容 */,第二种是/** 内容 */. 这两种多行注释的区别是/** 内容 */这种注释可以生成一个该文件的注释文档,下面是演示代码. A ...

  4. 20172333 2017-2018-2 《Java程序设计》第10周学习总结

    20172333 2017-2018-2 <Java程序设计>第10周学习总结 教材学习内容 第十三章 集合是一个对象,一个保存其他对象的数据库. 集合可以保存不同种类的对象也可以保存同种 ...

  5. NIO 服务端TCP连接管理的方案

    最近做的一个项目需要在服务端对连接端进行管理,故将方案记录于此. 方案实现的结果与背景 因为服务端与客户端实现的是长连接,所以需要对客户端的连接情况进行监控,防止无效连接占用资源. 完成类似于心跳的接 ...

  6. Ubuntu 配置 ftp freemind adb

    . 1. 配置apt-get源 配置过程 : sudo vim /etc/profile 命令, 在后面添加下面的内容; 刷新配置文件 : source /etc/profie 命令; 刷新源 : s ...

  7. LintCode-112.删除排序链表中的重复元素

    删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素每个元素只留下一个. 样例 给出 1->1->2->null,返回 1->2->null 给出 1-> ...

  8. oracle 9i 图文安装教程 oracle 9i 安装

    我的安装文件是ISO镜像文件,使用Virtual DAEMON Manager v 4.10打开: ora9i-1.iso ora9i-2.iso ora9i-3.iso 首先必须把上面三个镜像文件都 ...

  9. JDK版本Java SE、Java EE、Java ME的区别

    想在win7 X64上搭建JAVA开发环境来着(只是尝试下),打开JAVA 官网下载JDK,发现好多版本懵了,百度了下找到这些版本的区别,故有了下文 1.JAVA SE Java2平台标准版(Java ...

  10. WDCP V3.2面板安装且新增PHP多版本和免费Let's Encrypt SSL证书

    文章原文:http://www.itbulu.com/wdcp-v32.html 我们很多网友对于WDCP面板应该算是比较熟悉的,老蒋在博客中也多次分享WDCP面板的相关教程内容,因为在平时帮助网友解 ...