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. (原) MaterialEditor部- UmateriaEditor中 Node编译过程和使用(2)

    @白袍小道 转载说明原处 插件同步在GITHUB: DaoZhang_XDZ     需求: 1.梳理FexpressionInput和Output的编译和链接(套路和逻辑目的) 2.如何做到节点编译 ...

  2. Android开发-API指南-<activity>

    <activity> 英文原文:http://developer.android.com/guide/topics/manifest/activity-element.html 采集(更新 ...

  3. [Clr via C#读书笔记]Cp15枚举和位标识

    Cp15枚举和位标识 枚举类型 本质是结构,符号名称-值:好处显而易见:System.Enum;值类型: 编译的时候,符号会转换为常量字段: 枚举支持很多方法和成员: 位标识bit flag 判断和设 ...

  4. 算法模板の数学&数论

    1.求逆元 int inv(int a) { ) ; return (MOD - MOD / a) * inv(MOD % a); } 2.线性筛法 bool isPrime[MAXN]; int l ...

  5. 【转】Backbone.js学习笔记(一)

    文章转自: http://segmentfault.com/a/1190000002386651 基本概念 前言 昨天开始学Backbone.js,写篇笔记记录一下吧,一直对MVC模式挺好奇的,也对j ...

  6. css3美化radio样式

    .magic-radio{ position: absolute; display: none; } .magic-radio + label { position: relative; displa ...

  7. Java容器之Iterator接口

    Iterator 接口: 1. 所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象. 2. Iterator 对象称作迭代器,用以方便的 ...

  8. 详解实现Android中实现View滑动的几种方式

    注: 本文提到的所有三种滑动方式的完整demo:ScrollDemo 1. 关于View我们需要知道的 (1)什么是View? Android中的View类是所有UI控件的基类(Base class) ...

  9. 3dContactPointAnnotationTool开发日志(十三)

      为了使生成的项目能够显示报错信息我又勾选了下面这几个选项:   然后生成的项目运行时可以显示错误信息了,貌似是shader是空的.   之前的代码是这么写的,调用了Shader.Find(),貌似 ...

  10. PAT 1058 选择题

    https://pintia.cn/problem-sets/994805260223102976/problems/994805270356541440 批改多选题是比较麻烦的事情,本题就请你写个程 ...