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

题意:

稳定婚姻系统问题如下:

1)集合 M 表示 n 个男性

2)集合 F 表示 n 个女性

3)对于每个人我们都按异性的中意程度给出一份名单(从最中意的到最不中意的)

如果没有 ,f 对 m 比对她的配偶中意的同时 m 对 f 比对他的配偶更加中意,那么这个婚姻是稳定的。

如果一个稳定配对不存在另一个稳定婚姻配对,其中所有的男性的配偶都比现在强,那么这样的稳定配对称为男性最优配对。

给出 n 对男女,以小写字母表示男性名字,大写字母表示女性名字,再给出每个男性的中意名单(从大到小),最后是女性的中意名单(从大到小)。

对于每组测试数据输出一组男性最优配对,男性按字典序从小到大排序,每行为:“男姓名 女性名” 的形式。

思路:裸的延迟认可算法,此题为模版题

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 10001
#define MOD 123
#define E 1e-6
using namespace std;
int couple;//总共多少对
int Male_Like[N][N];//男性对女性的喜欢程度
int Female_Like[N][N];//女性对男性的喜欢程度
int Male_Choice[N];//男性的选择
int Female_Choice[N];//女性的选择
int Male_Name[N],Female_Name[N];//姓名的hash
queue<int> Free_Male;//没有配对的男性
char str[N];
int main()
{int t;while(scanf("%d",&t)!=EOF){scanf("%d",&couple);while(!Free_Male.empty())//清空队列Free_Male.pop();for(int i=0;i<couple;i++)//存入男性名字,初始都没有配对{scanf("%s",str);Male_Name[i]=str[0]-'a';Free_Male.push(Male_Name[i]);}for(int i=0;i<couple;i++)//存入女性名字,初始都没有配对{scanf("%s",str);Female_Name[i]=str[0]-'A';}sort(Male_Name,Male_Name+couple);//对名字排序for(int i=0;i<couple;i++)//男性对女性的印象,按降序排列{scanf("%s",str);for(int j=0;j<couple;j++)Male_Like[i][j]=str[j+2]-'A';}for(int i=0;i<couple;i++)//女性对男性的印象,添加一虚拟人物,编号为couple,为女性的初始对象{scanf("%s",str);for(int j=0;j<couple;j++)Female_Like[i][str[j+2]-'a']=couple-j;Female_Like[i][couple]=0;}memset(Male_Choice,0,sizeof(Male_Choice));//所有男性初始选择都是最喜欢的女性for(int i=0;i<couple;i++)//先添加女性的初始对象Female_Choice[i]=couple;while(!Free_Male.empty()){int male=Free_Male.front();//找出一个未配对的男性int female=Male_Like[male][Male_Choice[male]];//男性心仪的女性if(Female_Like[female][male]>Female_Like[female][Female_Choice[female]])//女性对男性的喜爱度大于当前对象{Free_Male.pop();//男性脱单if(Female_Choice[female]!=couple)//如果有前男友且不为虚拟对象couple{Free_Male.push(Female_Choice[female]);//其前男友进入队列,重新变为光棍Male_Choice[Female_Choice[female]]++;//其前男友考虑其下一对象}Female_Choice[female]=male;//当前男友为当前男性}else//如果被女性拒绝Male_Choice[male]++;//考虑下一对象}for(int i=0;i<couple;i++)printf("%c %c\n",Male_Name[i]+'a',Male_Like[Male_Name[i]][Male_Choice[Male_Name[i]]]+'A');printf("\n");}return 0;
}

The Stable Marriage Problem(POJ-3487)相关推荐

  1. HDU 1914 The Stable Marriage Problem (稳定婚姻匹配)

    题意:稳定婚姻匹配问题,板子题. 题解:稳定婚姻匹配 注意按字典序输出,还有输出格式,最后个样例不输出空行. #define _CRT_SECURE_NO_WARNINGS #include<i ...

  2. HDOJ 1914 The Stable Marriage Problem

    rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 6553 ...

  3. uva10401Injured Queen Problem(递推)

    题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现 ...

  4. P1601 A+B Problem(高精)-- python3实现

    A+B Problem(高精) - 洛谷 """ P1601 A+B Problem(高精)-- python3实现 https://www.luogu.com.cn/p ...

  5. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  6. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  7. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  8. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  9. AI绘画指南 如何设置与使用 stable diffusion webui (SD webui)

    分享最近对AI绘画的理解和实践经验,希望帮助那些对AI绘画有兴趣但不知如何入门的人.分享的内容主要包括对stable-diffusion-webui界面的介绍,解释参数的含义和如何进行调整.此外,还会 ...

最新文章

  1. CV圈太卷了!继谷歌提出MLP-Mixer之后,清华、牛津等学者又发表三篇MLP相关论文...
  2. CTFshow php特性 web132
  3. 强化学习笔记1:强化学习概述
  4. cocos2D(四)---- CCSprite
  5. (1-e^(-j5w))/(1-e^(-jw))=e^(-j2w)*sin(5w/2)/sin(w/2)的证明过程
  6. in会让mysql索引失效吗_mysql的in会不会让索引失效?
  7. 用户、组织结构、功能菜单、权限分配设计
  8. Windows 7 ship party
  9. SPSS(十五)spss之聚类分析(图文+数据集)
  10. 增量式光电编码器原理及其结构
  11. 最常用2000英语单词(带音标+注释)
  12. 异常解决:cococaption包出现找不到edu.stanford.nlp.semgraph.semgrex.SemgrexPattern错误
  13. python如何定义函数_python如何定义函数
  14. VS Code 引入pthread.h头文件
  15. 【Benewake(北醒) 】长距 TF03 100m/180m介绍以及资料整理
  16. 10.9 guz模拟题题解
  17. python自学免费教程-怎样自学python编程?从零开始学习python,python开发入门到精通
  18. 2021广工计算机考研,2021计算机考研大纲大纲什么时候公布
  19. bootstrap表格 行编辑状态_JS表格组件BootstrapTable行内编辑解决方案x-editable
  20. 2008年MBA全国联考英语考试大纲

热门文章

  1. 6个特征,判断你的领导值不值得追随
  2. 值得收藏!数据分析最常用的18个概念,终于有人讲明白了
  3. 爬虫小工具合集|不会编程也能爬数据
  4. java无限循环可变参数,Java可变参数、加强for循环
  5. docker添加jar包_Docker部署jar包
  6. wpf datepicker 选择时分秒_[Angular 组件库 NG-ZORRO 基础入门] - DatePicker
  7. P3-weixin-2.0.1 版本发布,JAVA微信插件框架
  8. jeecg公开课今晚主题:新版本功能介绍、online原理和代码生成器,欢迎大家报名
  9. 解决maven内存溢出
  10. MySQL进阶篇(02):索引体系划分,B-Tree结构说明