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. python 打包

    一.下载 pip install Pyinstaller 二.使用Pyinstaller 1.使用下载安装的方式安装的Pyinstaller打包方式 将需要打包的文件放在解压得到的Pyinstalle ...

  2. IDEA搭载Tomcat使用JSTL连接Oracle数据库

    1.在IDEA中,JSTL库添加到WEB-INF/lib下面可以直接在JSP页面上通过 <%@ taglib uri="http://java.sun.com/jsp/jstl/cor ...

  3. 使用Python客户端(redis-py)连接Redis--华为云DCS for Redis使用经验

    使用Python连接Redis,需要先安装Python以及redis-py,以CentOS为例,介绍redis-py的客户端环境搭建. 第0步:准备工作 华为云上购买1台弹性云服务器ECS(我选了Ce ...

  4. 从零开始的Python学习Episode 5——字典

    字典 字典是另一种可变容器模型,且可存储任意类型对象. 一.添加 (1)直接添加 dict={'name':'smilepup'} dict['age']=20 dict['name']='piggy ...

  5. Leetcode - 557. Reverse Words in a String III (C++) stringstream

    1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是 ...

  6. 三:Fair Scheduler 公平调度器

    参考资料: http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/FairScheduler.html http://h ...

  7. SGU 194 Reactor Cooling(无源无汇上下界可行流)

    Description The terrorist group leaded by a well known international terrorist Ben Bladen is bulidin ...

  8. 嵌入式码农的10年Bug调试经验,值得一看

    下面这些都是我经历过的会导致难点bug的问题: 1.事件顺序.在处理事件时,提出下列问题会很有成效:事件可以以不同的顺序到达吗?如果我们没有接收到此事件会怎么样?如果此事件接连发生两次会怎么样?哪怕通 ...

  9. 关于智能指针类型shared_ptr的计数问题

    一.关键 每个shared_ptr所指向的对象都有一个引用计数,它记录了有多少个shared_ptr指向自己 shared_ptr的析构函数:递减它所指向的对象的引用计数,如果引用计数变为0,就会销毁 ...

  10. 20145214 《Java程序设计》第10周学习总结

    20145214 <Java程序设计>第10周学习总结 学习内容总结 计算机网络概述 在计算机网络中,现在命名IP地址的规定是IPv4协议,该协议规定每个IP地址由4个0-255之间的数字 ...