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

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
 
Author
CHENG, Long
 
Source
题意:
n位男士,n位女士,没人对各个异性有一个排序,代表对他们的喜欢程度。任务是将男生和女生一一配对,使得男生v和女生u不存在以下情况:(1)男生v和女生u不是舞伴;(2)他们喜欢对方的程度都大于喜欢各自当前舞伴的程度。
输出最稳定配对,好像没有Impossible
代码:
//稳定婚姻匹配  详细:白书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 稳定婚姻匹配 模板的更多相关文章

  1. HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)

    <题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的 ...

  2. Marriage is Stable HDU1522 稳定婚姻问题基础

    几对男女   给出每个人心中的优先级   进行最合理的匹配 要打印名字的话必须有一个名字数组 英文名用map 稳定婚姻问题: 每次循环遍历所有的男的 每个男的对目前未被拒绝的并且优先级最高的进行预匹配 ...

  3. HDU1914 稳定婚姻匹配

    The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (J ...

  4. HDU 1522 Marriage is Stable 稳定婚姻匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1522 #include<bits/stdc++.h> #define INF 0x3f3f3f3f ...

  5. 图论补档——KM算法+稳定婚姻问题

    突然发现考前复习图论的时候直接把 KM 和 稳定婚姻 给跳了--emmm 结果现在刷训练指南就疯狂补档.QAQ. KM算法--二分图最大带权匹配 提出问题 (不严谨定义,理解即可) 二分图 定义:将点 ...

  6. CF1147F Zigzag Game & 稳定婚姻问题学习笔记

    CF1147F Zigzag Game 这题太神仙了,不得不记录一下. 我网络流做不动了,DS做不动了,DP做不动了,特别自闭.于是博弈论之神(就是随手切3500博弈的那种) \(\color{bla ...

  7. 【稳定婚姻问题】【HDU1435】【Stable Match】

    2015/7/1 19:48 题意:给一个带权二分图  求稳定匹配 稳定的意义是对于某2个匹配,比如,( a ---- 1) ,(b----2) , 如果 (a,2)<(a,1) 且(2,a)& ...

  8. 稳定婚姻问题和Gale-Shapley算法(转)

    什么是算法?每当有人问作者这样的问题时,他总会引用这个例子:假如你是一个媒人,有若干个单身男子登门求助,还有同样多的单身女子也前来征婚.如果你已经知道这些女孩儿在每个男孩儿心目中的排名,以及男孩儿们在 ...

  9. UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)

    题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...

随机推荐

  1. [译] JavaScript核心指南(JavaScript Core) 【转】

    本文转自:http://remember2015.info/blog/?p=141#scope-chain 零.索引 对象(An Object) 原型链(A Prototype Chain) 构造函数 ...

  2. OSS文件上传及OSS与ODPS之间数据连通

    场景描述        有这样一种场景,用户在自建服务器上存有一定数量级的CSV格式业务数据,某一天用户了解到阿里云的OSS服务存储性价比高(嘿嘿,颜值高),于是想将CSV数据迁移到云上OSS中,并且 ...

  3. opencv-学习笔记(5)形态学转变

    opencv-学习笔记(4)形态学转变 本章讲了几种形态学操作 腐蚀erode 膨胀dilate 开运算MORPH_OPEN 闭运算MORPH_CLOSE 形态学梯度MORPH_GRADIENT 礼帽 ...

  4. Linux内核设计笔记7——中断

    中断与中断处理 何为中断? 一种由设备发向处理器的电信号 中断不与处理器时钟同步,随时可以发生,内核随时可能因为中断到来而被打断. 每一个中断都有唯一一个数字标志,称之为中断线(IRQ) 异常是由软件 ...

  5. [leetcode-718-Maximum Length of Repeated Subarray]

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  6. nodejs笔记--模块篇(三)

    文件模块访问方式通过require('/文件名.后缀')    require('./文件名.后缀')    requrie('../文件名.后缀') 去访问,文件后缀可以省略:以"/&qu ...

  7. Python中的名字隐藏

    Python对于module文件中的name是没有private和public区分的,严格来说,在module文件重定义的任何name,都可以被外界访问.但是,对于 from module imort ...

  8. 《剑指Offer》题四十一~题五十

    四十一.数据流中的中位数 题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中 ...

  9. 将代码上传到GitHub

    网上看了很多资料,都是用的命令行,比较难看懂,自己摸索了一下怎么样在图形界面上操作.下面记录的只是简单的如何把本地仓库直接上传到服务器上. 在mac上下载个GitHub Mac客户端,安装好后运行,输 ...

  10. SOA架构的理解

    实践论认为:从实践提升到理论,再由理论指导实践,由此向前发展.目前SOA的发展的情况………… 通过不少实践,SOA的模型己经被公认为标准规范,目前是正需要进一步总结上升到理论的时候了. SOA架构的演 ...