2019-02-25-The Stable Marriage Problem-POJ3487-稳定婚姻算法

HDU-1914-The Stable Marriage Problem(稳定婚姻算法) The Stable Marriage Problem 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 in... 阅读详情

POJ-3487-The Stable Marriage Problem

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

题目简述

对于给定的n对男女,再给出相应的好感度排名,即男生眼中女生的好感度排名(n行)和女生眼中男生的好感度排名(n行)。

对于两个匹配a-b,c-d,如果a的眼中d的排名比b高,d的眼中a排名比c高,那么这样的匹配不稳定。

试求出一种匹配方式,使得对于所有的男生和女生,都是一个稳定的匹配。

条目检索:稳定婚姻算法

输入输出

输入:无难点,字符串读入即可

输出:无难点

使用模板(Gale_Shapley稳定婚姻算法)

struct Gale_Shapley{
	static const int N=60;
	int n;
	int fg;
	int woman[N][N];        //第i个girl对编号j的boy的好感排位 
	int man[N][N];          //第i个boy第j喜欢的girl 
	int man_match[N],woman_match[N];    //已匹配的男女 
	int p[N];               //第i个boy已经表白的人数 
	void solve(){
		for(int i=0;i<N;i++)p[i]=1;
		memset(man_match,0,sizeof(man_match));
		memset(woman_match,0,sizeof(woman_match));
		fg=true;
		while(fg){
			fg=false;
			for(int i=1;i<=n;i++){
				if(!man_match[i]){
					int t=man[i][p[i]++];
					if(!woman_match[t]){
						man_match[i]=t;
						woman_match[t]=i;
					}else if(woman[t][woman_match[t]]>woman[t][i]){
						man_match[woman_match[t]]=0;
						man_match[i]=t;
						woman_match[t]=i;
					}
					fg=true;
				}
			}
		}
	}
}T;

AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
//使用模板
struct Gale_Shapley{
	static const int N=60;
	int n;
	int fg;
	int woman[N][N];//第i个girl对编号j的boy的好感排位 
	int man[N][N];//第i个boy第j喜欢的girl 
	int man_match[N],woman_match[N];//已匹配的男女 
	int p[N];//第i个boy已经表白的人数 
	void solve(){
		for(int i=0;i<N;i++)p[i]=1;
		memset(man_match,0,sizeof(man_match));
		memset(woman_match,0,sizeof(woman_match));
		fg=true;
		while(fg){
			fg=false;
			for(int i=1;i<=n;i++){
				if(!man_match[i]){
					int t=man[i][p[i]++];
					if(!woman_match[t]){
						man_match[i]=t;
						woman_match[t]=i;
					}else if(woman[t][woman_match[t]]>woman[t][i]){
						man_match[woman_match[t]]=0;
						man_match[i]=t;
						woman_match[t]=i;
					}
					fg=true;
				}
			}
		}
	}
}T;

int main(){
	int cases;
	scanf("%d",&cases);
	while(cases--){
		scanf("%d",&T.n);
		char boy[27];         //编号对应的字母
		char girl[27];        //编号对应的字母
		char s[30];
		map<char,int>bo,gi;   //字母对应的编号
        //建立映射
		for(int i=1;i<=T.n;i++){
			scanf("%s",s);
			boy[i]=s[0];
			bo[s[0]]=i;
		}
		for(int i=1;i<=T.n;i++){
			scanf("%s",s);
			girl[i]=s[0];
			gi[s[0]]=i;
		}
        //读入排名
		for(int i=1;i<=T.n;i++){
			scanf("%s",s);
			int x,y;
			x=bo[s[0]];
			for(int j=1;j<=T.n;j++){
				y=gi[s[1+j]];
				T.man[x][j]=y;      //男生x所第j喜欢的女生是y
			}
		}
		for(int i=1;i<=T.n;i++){
			scanf("%s",s);
			int x,y;
			x=gi[s[0]];
			for(int j=1;j<=T.n;j++){
				y=bo[s[1+j]];
				T.woman[x][y]=j;    //对于女生x而言男生y的排名是j
			}
		}

		T.solve();

		for(int i=1;i<=T.n;i++){
			printf("%c %c\n",boy[i],girl[T.man_match[i]]);
		}
		if(cases!=0)printf("\n");
	}
	return 0;
}

参考链接

https://blog.youkuaiyun.com/u011815404/article/details/81395124

https://blog.youkuaiyun.com/cscmaker/article/details/8291131

稳定婚姻问题:Gale–Shapley算法 (一)问题的引出 在组合数学、经济学、计算机科学中,稳定婚姻问题(英语:stable marriage problem,简称SMP)又称为稳定配对问题(stable matching problem),是指在两组同样大小的元素集合中(例如集合1是男子组、集合2是女子组,而他们各有偏好),寻求一个稳定配对组合所遇到的问题。一个组合在以下情况下并不稳定: 在集合1中有一个元素A更偏好于集合2的一些元素B,但元素A已被配对;而元素B亦更偏好于元素A多于配对给他的元素。在男女婚姻的角度来说,可以写成一名男子A获安排 阅读详情

相关推荐

【稳定匹配】:poj3487,The Stable Marriage Problem

http://poj.org/problem?id=3487 Gale-Shapley算法求稳定婚姻 # include # include # include using namespace std; # define N 30 int n; char m[N],w[N]; vector > mL,wL,MproposeW; map MmatchW; map WmatchM;

mmc2015的专栏 1327

【hdu】The Stable Marriage Problem(婚姻匹配问题/稳定匹配)

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

qianzhezhi1127的博客 932

POJ3487 The Stable Marriage Problem

稳定婚姻问题

Monster__Yi的博客 1960

poj 3487

The Stable Marriage Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1959   Accepted: 835 Description The stable marriage problem consists of matching

sdjzujxc的专栏 1645

【稳定婚姻问题】poj3487 The Stable Marriage Problem

具体参见上篇博客。 都是类似的问题,不过我发现这类问题好像输入格式的控制会稍微难一点。 Gale-shapley的实现会相对简单很多。其中求loc部分其实也可以用map优化一下,否则我的实现是可能退化到O(n^3)的。 当然这问题规模实在是太小了,所以也就不需要优化。。。 #include #include #include #include #include #include

When you go away 895

用Python实现单层感知器:5分钟搞定逻辑与/或/异或分类(附避坑指南)

本文通过Python代码实战,详细讲解了单层感知器的核心原理与实现步骤,5分钟内即可完成逻辑与、或运算的分类任务。文章深入剖析了单层感知器的能力边界,解释了其无法解决异或(XOR)等线性不可分问题的根本原因,并提供了学习率设置、权重初始化等关键避坑指南,帮助初学者直观理解神经网络的基础。

ttt77的博客 1084

算法 | 盖尔-沙普利(Gale-Shapley)婚姻稳定匹配算法

本文将要介绍的盖尔-沙普利稳定匹配算法是一个有趣的算法,可以用来解决某些方面的问题或至少也能提供一些思路:比如男女双方互相挑选对象,或用人单位和毕业生互相选择,或者是在学生选课时等等。本文将仔细说明该算法的原理,并用MATLAB编程演示如何实现这一算法,最后得出若干结论。文章的主要内容包括背景说明、原理及思路、程序实现以及结果分析。

Chen_Tianyang的博客 2万+

NSX-T 4.1部署搭建

NSX-T4.1 部署搭建

weixin_58250887的博客 564

POJ 3487 The Stable Marriage Problem(Gale-Shapley算法求稳定婚姻)

转载请注明出处,谢谢http://blog.youkuaiyun.com/acm_cxlove/article/details/7854526       by---cxlove 题目:有N位女士和N位男士,每位男士或者女士都对对方有个评价。他们会形成N对夫妻,如果A和a结婚,B和b结婚,但是A更偏爱b而非a而且b也更偏爱A而非B,那么这种婚姻是不稳定的 http://poj.org/problem?

窝不是爱酱,喵~~~~ 3460

一文吃透 OData Deep Insert:从框架原理到 ABAP 实战

本文面向ABAP工程师,解析OData服务中的Deep Insert技术。文章首先阐述Deep Insert的业务背景,说明其适用于父子层级数据必须成组创建的场景(如销售订单与行项目)。随后深入框架实现机制,介绍SAP Gateway如何通过CREATE_DEEP_ENTITY方法处理嵌套请求,并强调开发者的责任边界。通过销售订单案例,文章对比了Deep Insert与$batch的适用场景差异,提供请求负载示例和DPC_EXT实现骨架代码。最后给出MPC_EXT声明深结构的三种套路,以及事务处理和错误处理

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 225

MATLAB与ROS联合仿真——控制类功能模块介绍

MATLAB与ROS联合仿真——控制类功能模块介绍

慕羽★的博客 2353

poj 3487 The Stable Marriage Problem(稳定婚姻问题)

The Stable Marriage Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2130   Accepted: 908 Description The stable marriage problem consists of matching

WEYuLi的专栏 1584

华为od 面试八股文_Python_05_含答案

华为od 面试八股文_Python_05_含答案

MISAYAONE的博客 2560

#POJ 3487 The Stable Marriage Problem (稳定婚姻问题)

The Stable Marriage Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3890 Accepted: 1626 Description The stable marriage problem consists of matching members of...

OlaMins 771

模型泛化能力提升:领域自适应与迁移学习

模型泛化能力提升:领域自适应与迁移学习​ ,人工智能,计算机视觉,大模型,AI,在机器学习和深度学习的应用中,模型的泛化能力是一个关键问题。泛化能力指的是模型在新的、未见过的数据上表现良好的能力。然而,实际情况中,训练数据和测试数据往往来自不同的领域,存在分布差异,这会导致模型的泛化能力下降。领域自适应与迁移学习正是为了解决这一问题而发展起来的重要技术。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 3万+

麒麟服务器操作系统QT系列软件工具手册

对Qt提供的开发工具进行了一系列的优化之后,在日常的修复bug和维护框架的过程中,桌面研发对Qt的基础软件库也进行了部分优化工作,包括qtbase、qtwayland、qtscript、qt4-x11等Qt基础组件,涉及的Qt框架版本主要涉及到5.9和5.12两个版本。qt 的翻译,是由 ts 文件生成 qm 文件然后进行翻译,国产操作系统上qt 的翻译是由单独的qttranslations包中进行维护,其次部分汉化问题也是qt组件维护工作的一部分,qt的翻译机制如图4-7所示。

太极淘的博客 543

poj3487 The Stable Marriage Problem(最稳定的婚姻)

The Stable Marriage Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1966   Accepted: 838 Description The stable marriage problem consists of matching

a451907的专栏 948

基于YOLOv11与相机标定的齿轮尺寸测量系统

本文详细介绍了一种基于深度学习的目标检测算法YOLOv11与相机标定技术相结合的齿轮尺寸自动测量系统。该系统能够实时检测图像中的齿轮目标,并精确测量其关键尺寸参数,包括齿顶圆直径、齿根圆直径、模数等。我们构建了完整的深度学习解决方案,包含数据采集与标注、模型训练、相机标定、尺寸测量算法以及用户友好的UI界面。本文提供了完整的代码实现,并推荐了相关的参考数据集,为工业视觉检测领域提供了实用的技术方案。关键词:YOLOv11;齿轮检测;尺寸测量;相机标定;深度学习;工业视觉python。

YOLO 411

POJ 3487 The Stable Marriage Problem

稳定婚姻问题【Gale-Shapley算法】

YHaX的博客 533

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

poj 3487 The Stable Marriage 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...

aoanping0730的博客 178

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

POJ 3487 The Stable Marriage Problem(稳定婚姻问题)

#include #include #include #include #include #include using namespace std; const int maxn=27+5; int pref[maxn][maxn],order[maxn][maxn],Next[maxn]; int future_husband[maxn],future_wife[maxn]; queue q;

qq_33901573的博客 691

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

poj3487-The Stable Marriage Problem

poj3487-The Stable Marriage ProblemThe Stable Marriage Problem Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 3084Accepted: 1311 Description The stable marriage problem consists of matching

weixin_40433319的博客 525

POJ 3487 The Stable Marriage Problem (稳定婚姻问题)

题意:http://poj.org/problem?id=3487 /*男生向女生求爱,男生最优稳定匹配*/ #include #include #include using namespace std; #define MAXN 50 int Mpref[MAXN][MAXN]; // Mpref[i][j]表示男生i第j偏爱的女生 int Wpref[MAXN][MAXN];

2222

POJ3487 The Stable Marriage Problem(稳定婚姻问题)(Gale-Shapley算法)

The Stable Marriage Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3274   Accepted: 1401 Description The stable marriage problem...

pxlsdz的博客 1620

稳定婚姻问题 Poj 3487 The Stable Marriage Problem + Hdu 1522 Marriage is Stable (二分图稳定匹配)

貌似又叫延迟认可算法? 问题模型及解答方法可以参考: 稳定婚姻问题 [0]引言 什么是算法 如何寻找稳定的婚姻搭配 大学招生问题模型也可以参考这个。 Poj 3487 The Stable Marriage Problem #include #include #include #include using namespace std; const int N=30

whyorwhnt的专栏 1865

POJ 3487 The Stable Marriage Problem 稳定婚姻问题

From: http://hi.baidu.com/acmdearway/blog/item/cd6804fdf1098888b901a01d.html/cmtid/132effa989853bbecb130cde 稳定婚姻是组合数学里面的一个问题。 问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序,同时每位男生也按照自己的偏爱程度将女生排序。然后将这n

行风 755
上一篇: 2019-02-25-方格取数(1)-HDU1565-dp-状态压缩
下一篇: 模板-Gale_Shapley稳定婚姻算法
silzero
博客等级 码龄8年 3粉丝 12原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值