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. 【20180807模拟测试】T2 box

    [问题描述] 有个桌子长 R 宽 C,被分为 R*C 个小方格.其中,一些方格上有箱子,一些方格上有按 钮,一些方格上有障碍物,一些方格上是空地.现在有个任务,需要把所有箱子推到这些按 钮上面.箱子有 ...

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

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

  3. 关于GitHub推送时发生Permission denied (publickey)的问题

    今天在学习廖雪峰老师官网的git教程“添加远程库”时发现总是推送失败,下边提示“Permission denied (publickey) 这个问题” 传送门:https://www.liaoxuef ...

  4. SpringBoot在IDEA下使用JPA

    1依赖 使用IDEA构建基于JPA的项目需要引用JPA.MYSQL依赖 2配置文件修改 2.1连接库 spring.datasource.url=jdbc:mysql://localhost:3306 ...

  5. 6.azkban的监控

    azkaban自带的监控flow自带的邮件功能SLA总结写程序监控job情况监控azkaban的元数据库使用azkaban API监控总结 azkaban自带的监控 azkban目前仅仅支持邮件监控, ...

  6. [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 ...

  7. 微信小程序学习:开发注意点

    11月2日更新: 微信小程序支持内嵌网页,新增 <web-view /> 组件调试支持: 传送门 <!-- wxml --> <!-- 指向微信公众平台首页的web-vi ...

  8. JS中Document节点总结

    document对象是documentHTML的一个实例,也是window对象的一个属性,因此可以将document对象作为一个全局对象来访问. Document节点的子节点可以是DocumentTy ...

  9. ACM 第十七天

    暑期热身赛 BAPC 2014 The 2014 Benelux Algorithm Programming Contest 题目网址:https://odzkskevi.qnssl.com/3655 ...

  10. java.lang.ClassNotFoundException: com.google.gson.Gson 问题解决

    我是这么解决:把gson.jar放到WEB-INF/lib目录下. 放在其他目录就会报错.