Marriage is Stable

2023-10-28

http://acm.hdu.edu.cn/showproblem.php?pid=1522

Problem Description
Albert, Brad, Chuck are happy bachelors who are in love with Laura, Marcy, Nancy. They all have three choices. But in fact, they do have some preference in mind. Say Albert, he likes Laura best, but that doesn't necesarily mean Laura likes him. Laura likes Chuck more than Albert. So if Albert can't marry Laura, he thinks Nancy a sensible choice. For Albert, he orders the girls Laura > Nancy > Marcy.

For the boys:

Albert: Laura > Nancy > Marcy
Brad: Marcy > Nancy > Laura
Chuck: Laura > Marcy > Nancy

For the girls:

Laura: Chuck > Albert > Brad
Marcy: Albert > Chuck > Brad
Nancy: Brad > Albert > Chuck

But if they were matched randomly, such as

Albert <-> Laura
Brad <-> Marcy
Chuck <-> Nancy

they would soon discover it's not a nice solution. For Laura, she likes Chuck instead of Albert. And what's more, Chuck likes Laura better than Nancy. So Laura and Chuck are likely to come together, leaving poor Albert and Nancy.

Now it's your turn to find a stable marriage. A stable marriage means for any boy G and girl M, with their choice m[G] and m[M], it will not happen that rank(G, M) < rank(G, m[G])and rank(M, G) < rank(M, m[M]).
 

Input
Each case starts with an integer n (1 <= n <= 500), the number of matches to make.

The following n lines contain n + 1 names each, the first being name of the boy, and rest being the rank of the girls.

The following n lines are the same information for the girls.

Process to the end of file.
 

Output
If there is a stable marriage, print n lines with two names on each line. You can choose any one if there are multiple solution. Print "Impossible" otherwise.

Print a blank line after each test.
 

Sample Input
  
  
3 Albert Laura Nancy Marcy Brad Marcy Nancy Laura Chuck Laura Marcy Nancy Laura Chuck Albert Brad Marcy Albert Chuck Brad Nancy Brad Albert Chuck
 

Sample Output
  
  
Albert Nancy Brad Marcy Chuck Laura
http://acm.hdu.edu.cn/showproblem.php?pid=1522

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<map>
using namespace std;
#define NN 600
int mm[NN][NN],gg[NN][NN],p[NN];
int gg_match[NN],mm_match[NN];//gg_match[i]=j;表示第i个gg喜欢第j个mm;
string ss[NN*2],str;
map<string,int>mp_mm,mp_gg;
int main()
{
	int i,j,k,m,n,cnt,a,b;
	while(scanf("%d",&n)!=EOF)
	{
		mp_gg.clear();
		mp_mm.clear();
		cnt=0;
		for(i=1;i<=n;i++)
		{
			cin>>ss[i];
			mp_gg[ss[i]]=i;
			for(j=1;j<=n;j++)
			{
				cin>>str;
				if(mp_mm[str]==0)
				{
					mp_mm[str]=++cnt;
					ss[n+cnt]=str;	
				}
				gg[i][j]=mp_mm[str];
			}
		}
		for(i=1;i<=n;i++)
		{
			cin>>str;
			a=mp_mm[str];
			for(j=1;j<=n;j++)
			{
				cin>>str;
				b=mp_gg[str];
				mm[a][b]=j;
			}
		}
		int flag=1;
		for(i=1;i<=n;i++)
		{
			p[i]=1;
			mm_match[i]=gg_match[i]=-1;
		}
		while(flag)
		{
			flag=0;
			for(i=1;i<=n;i++)
			{
				if(gg_match[i]==-1&&p[i]<=n)
				{
					a=gg[i][p[i]++];
					if(mm_match[a]==-1)
					{
						mm_match[a]=i;
						gg_match[i]=a;
					}
					else if(mm[a][i]<mm[a][mm_match[a]])
					{
						gg_match[i]=a;
						gg_match[mm_match[a]]=-1;
						mm_match[a]=i;
					}
					flag=1;
				}
			}
		};
		for(i=1;i<=n;i++)
		{
			j=gg_match[i];
			//printf("%d\n",j);
			cout<<ss[i]<<" "<<ss[j+n]<<endl;
		}
		cout<<"\n";
	}
	return 0;
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Marriage is Stable 的相关文章

随机推荐

  • STM32无人机-四轴四元数姿态解算与卡尔曼滤波

    四轴四元数姿态解算 MPU6050是一种非常流行的空间运动传感器芯片 可以获取器件当前的三个加速度分量和三个旋转角速度 什么是四元数 这部分很难 新手知道四元数的功能是将 6 轴传感器数据转化为三轴姿态角度数据即可 四元数解算程序店家已经封
  • 机器学习——决策树+剪枝(适用ID3与C4.5)

    问 标准的ID3算法支持剪枝操作 答 错误 标准的ID3算法不支持剪枝操作 该算法通过递归地构建决策树 在每个节点上使用信息增益作为判定条件进行特征选择 直到遍历完所有特征或者将数据集划分为同一类别的样本 ID3算法容易产生过拟合现象 剪枝
  • 记录一次NestedScrollView嵌套RecyclerView再嵌套RecyclerView的坑

    由于要做一些复杂的界面 需要在NestedScrollView下嵌套RecyclerView 在RecyclerView的条目中又有一个横向的RecyclerView 在 gt Android 7 0系统当中运行是显示正常的 但是在低于7
  • ctfshow 限时活动 红包挑战7和红包挑战8详细答案和见解

    ctfshow 利用create function函数 并绕过base64编码 highlight file FILE error reporting 0 extract GET create function name base64 en
  • 大数据工程师和Java后台开发的技术要求区别

    每家公司对大数据工作和java开发的要求不尽相同 目前长期从事数据库管理 挖掘 编程工作的人 包括传统的量化分析师 hadoop方面的工程师 以及任何在工作中需要通过数据来进行判断决策的管理者 比如某些领域的运营经理等 都可以尝试大数据工程
  • linux:命令行 &&与

    参考 Linux 命令行 与 简书 总结 command1 command2 只有前面命令执行成功 后面命令才继续执行 shell中 左边的命令 命令1 返回真 即返回0 成功被执行 后 右边的命令 命令2 才能够被执行 command1
  • mtk camera 移植步骤

    mtk camera 移植步骤 1 Kernel层驱动代码文件添加 mediatek custom doov92 wet tdd kernel imgsensor 下添加imx179 mipi raw 2lane 目录如下 imx179 m
  • 视频监控系统时间显示常见故障分析 及时间同步解决方案

    分别任职湖北三峡职业技术学院电子信息学院教研室主任 宜昌市教育技术装备站网络中心主任 宜昌市公安局科研所所长 视频监控系统是指综合应用视音频监控 通信 计算机网络等技术监视设防区域 并实时显示 记录现场图像的电子系统或网络 系统可以在非常事
  • Ubuntu 22.04部署Kubernetes 1.25

    Ubuntu 22 04部署Kubernetes 1 25 基本环境 系统和软件版本 master节点ip 安装过程 1 准备工作 1 1 修改主机名 1 2 关闭swap分区 1 3 关闭防火墙 1 4 重启电脑 确认swap和防火墙均已
  • angular组件封装

    1 这个公共组件的封装 2 c dropdown component ts import Component OnInit Input EventEmitter Output ViewChild from angular core Comp
  • 08LinuxC线程学习之pthread_join函数以及根据参2获取返回值的案例

    1 pthread join函数 int pthread join pthread t thread void retval 功能 阻塞等待线程退出 获取线程退出状态 其作用 对应进程中 waitpid 函数 成功 0 失败 错误号 参1
  • 1033. 旧键盘打字

    1033 旧键盘打字 20 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN Yue 旧键盘上坏了几个键 于是在敲一段文字的时候 对应的字符就不会出现 现在给出应该输
  • 【笔记】Nginx+Ngrok实现80端口服务器+80端口内网穿透

    安装ngrok 笔记 ngrok安装方法 安装完毕后ngrok默认将服务器的80端口占用 这时 需要修改启动脚本 vim etc init d ngrokd 找到如下部分 nohup sudo bin ngrokd tlsKey serve
  • 【每日一题】-金牌榜排序

    文章目录 题目描述 输入 输出 样例 解析 代码 题目描述 2012伦敦奥运会即将到来 大家都非常关注奖牌榜的情况 现在我们假设奖牌榜的排名规则如下 1 首先gold medal 数量多的排在前面 2 其次silver medal 数量多的
  • SpringBoot中 Lua函数操作redis

    Lua Lua 是一个简洁 轻量 可扩展的脚本语言 它的特性有 轻量 源码包只有核心库 编译后体积很小 高效 由 ANSI C 写的 启动快 运行快 内嵌 可内嵌到各种编程语言或系统中运行 提升静态语言的灵活性 如 OpenResty 就是
  • xman的思维导图快捷键_这个良心好用的思维导图软件,居然不用氪金充钱

    今天给大家介绍一款免费的在线思维导图工具 GitMind 提供了丰富的功能和模板 可免费导出 JPG PNG 图片 PDF 文档以及 TXT 文本等多种格式 此外 GitMind 还集成了制作流程图的能力 网站展示的流程图示例有泳道图 拓扑
  • Springboot项目使用达梦数据库

    下载达梦数据库驱动 Dm7JdbcDriver16 jar 执行maven命令把驱动包打入本地maven仓库 mvn install install file DgroupId com dm DartifactId DmJdbcDriver
  • 学校计算机如何脱控,学校机房脱控方法(已控状态)/极域电子教室脱离老师控制图文教程...

    老师没控制的时候 刀友应该都会断掉控制吧 我就不说了 就说说老师老师已经控制了该如何脱离控制 拔网线比较麻烦就不说了 以下操作之前先检查极域电子教室 右键右下角极域电子教室端 打开设置 把禁止结束学生端进程前面的勾去掉 把断网锁屏前面的勾去
  • 部署ELFK

    目录 ELFK ES logstash filebeat kibana 环境准备 所有节点 Elasticsearch 集群部署 在Node1 Node2节点上操作 修改elasticsearch主配置文件 es 性能调优参数 启动elas
  • Marriage is Stable

    http acm hdu edu cn showproblem php pid 1522 Problem Description Albert Brad Chuck are happy bachelors who are in love w