HDU1522 稳定婚姻匹配 模板
Marriage is Stable
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1047 Accepted Submission(s): 563
Special Judge
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]).
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.
Print a blank line after each test.
n位男士,n位女士,没人对各个异性有一个排序,代表对他们的喜欢程度。任务是将男生和女生一一配对,使得男生v和女生u不存在以下情况:(1)男生v和女生u不是舞伴;(2)他们喜欢对方的程度都大于喜欢各自当前舞伴的程度。
//稳定婚姻匹配 详细:白书352页
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
const int maxn=;
int n;
int pref[maxn][maxn],fum[maxn],fuw[maxn],ord[maxn][maxn],nex[maxn];
//pref:编号为i的man第j喜欢的woman的编号;fum:编号为i的woman的未婚夫;
//ord:编号为i的woman第j喜欢的man;
map<string,int>vm,vw;
map<int,string>um,uw;
queue<int>q;//未订婚的男士队列
//订婚函数
void engage(int m,int w)
{
int pm=fum[w];
if(pm){ //woman现有未婚夫
fuw[pm]=;//抛弃pm
q.push(pm);//pm加入未定婚男士队列
}
fuw[m]=w;
fum[w]=m;
}
int main()
{
char ch[];
while(~scanf("%d",&n)){
vm.clear();vw.clear();um.clear();uw.clear();
memset(pref,,sizeof(pref));
int cnt=;
for(int i=;i<=n;i++){
scanf("%s",ch);
vm[ch]=i;um[i]=ch;
for(int j=;j<=n;j++){
scanf("%s",ch);
if(!vw[ch]){
vw[ch]=++cnt;
uw[cnt]=ch;
}
pref[i][j]=vw[ch];//编号为i的男士第j喜欢的人
}
nex[i]=;//接下来应向排名为1的女士求婚
fuw[i]=;//没有未婚妻
q.push(i);
}
for(int i=;i<=n;i++){
scanf("%s",ch);
int k=vw[ch];
for(int j=;j<=n;j++){
scanf("%s",ch);
ord[k][vm[ch]]=j;//在编号为k的女士心中编号为vm[ch]的男士的排名
}
fum[k]=;//没有未婚夫
}
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++){
cout<<um[i]<<" "<<uw[fuw[i]]<<endl;
}
}
return ;
}
HDU1522 稳定婚姻匹配 模板的更多相关文章
- HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)
<题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的 ...
- Marriage is Stable HDU1522 稳定婚姻问题基础
几对男女 给出每个人心中的优先级 进行最合理的匹配 要打印名字的话必须有一个名字数组 英文名用map 稳定婚姻问题: 每次循环遍历所有的男的 每个男的对目前未被拒绝的并且优先级最高的进行预匹配 ...
- HDU1914 稳定婚姻匹配
The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (J ...
- HDU 1522 Marriage is Stable 稳定婚姻匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1522 #include<bits/stdc++.h> #define INF 0x3f3f3f3f ...
- 图论补档——KM算法+稳定婚姻问题
突然发现考前复习图论的时候直接把 KM 和 稳定婚姻 给跳了--emmm 结果现在刷训练指南就疯狂补档.QAQ. KM算法--二分图最大带权匹配 提出问题 (不严谨定义,理解即可) 二分图 定义:将点 ...
- CF1147F Zigzag Game & 稳定婚姻问题学习笔记
CF1147F Zigzag Game 这题太神仙了,不得不记录一下. 我网络流做不动了,DS做不动了,DP做不动了,特别自闭.于是博弈论之神(就是随手切3500博弈的那种) \(\color{bla ...
- 【稳定婚姻问题】【HDU1435】【Stable Match】
2015/7/1 19:48 题意:给一个带权二分图 求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...
- 稳定婚姻问题和Gale-Shapley算法(转)
什么是算法?每当有人问作者这样的问题时,他总会引用这个例子:假如你是一个媒人,有若干个单身男子登门求助,还有同样多的单身女子也前来征婚.如果你已经知道这些女孩儿在每个男孩儿心目中的排名,以及男孩儿们在 ...
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
随机推荐
- sparksql读写hbase
//写入hbase(hfile方式) org.apache.hadoop.hbase.client.Connection conn = null; try { SparkLog.debug(" ...
- Dev c++ 调试步骤
不能调试的时候,修改下列地方: 1.在“工具”->编译选项->”Add following commands when calling complier”下面的编辑框里写入:-g3 2.在 ...
- C#程序 权限不够的解决方案
有时候需要操作硬件,或者启动windows服务程序时,系统会提示很多奇怪的问题,归根结底就是程序当前所拥有的权限不够,需要提升,以前我们时手写一个manifest,多不容易啊, 现在有正常的方法了 1 ...
- JavaScript初探系列之String的基本操作
1.字符串转换 字符串转换是最基础的要求和工作,你可以将任何类型的数据都转换为字符串,你可以用下面三种方法的任何一种: var myStr = num.toString(); // "19& ...
- 第二章 shell的语法
变量:字符串.数字.环境和参数 获取变量内容可以在变量前使用$字符,使用echo指令可以将变量内容输出到终端. wuchao@wuchao-Lenovo:~$ var=hello wuchao@wuc ...
- iOS- <项目笔记>项目配置常见文件
项目常见文件 1.main.m * 里面有一个程序的入口:main函数 2.Prefix.pch文件 * pch文件中的内容能被项目中的其他任何文件共享\包含\访问 * 如果定义的内容只用在OC环境中 ...
- TensorFlow安装及jupyter notebook配置
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:TensorFlow安装及jupyter notebook配置 本文地址:http:/ ...
- hibernate映射表
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibern ...
- 【Asp.Net】IIS应用程序池添加ASP.NET v4.0
可能在安装.NET Framewrok 4.0之前,IIS就已经装好了,结果在IIS的应用程序池中只有.NET 2.0的Classic .NET AppPool和DefaultAppPool.在使用v ...
- 【Python】Python time mktime()方法
描述 Python time mktime() 函数执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数. 如果输入的值不 ...