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. 【转载】appium 操作汇总

    '''.appium api第二弹 锋利的python,这是初稿,2015/1/5 如有错误的地方,请同学们进行留言,我会及时予以修改,尽量整合一份ok的api 作者:Mads Spiral QQ:7 ...

  2. 浅谈PCA

    最近在回顾PCA方面的知识,发现对于之前的很多东西有了新的理解,下面和大家分享下我的一些个人的理解 1.我们为什么要用PCA,它能解决我什么问题? PCA(Principal Component An ...

  3. LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )

    1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的 ...

  4. 特殊符号 & 以太坊

    &表示取二进制的末尾 &1表示如果末尾是奇数和偶数两种情况 0 偶数 1奇数 举例子: int a=1;int p=&a; 其中,p是指针,&a就是将a在内存中的实际地 ...

  5. Netcore logging config

  6. javascript对table的添加,删除行的操作

    <body> <form name="myForm"> <table width="100%" id="tab" ...

  7. js如何处理字符串中带有↵字符

    js或vue中如何处理字符串中带有↵字符 split('\n') 使用split('\n')将字符串分割成数组就行 如果我们在vue中,只需要在页面中绑定变量时操作split('\n')就可以了: & ...

  8. POSIX线程学习

    一.什么是线程 在一个程序中的多个执行路线就叫做线程.更准确的定义是:线程是一个进程内部的一个控制序列.所有的进程都至少有一个线程.当进程执行fork调用时,将创建出该进程的一份新副本,这个新进程拥有 ...

  9. lintcode-156-合并区间

    156-合并区间 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [ ...

  10. iOS- 优化与封装 APP音效的播放

    1.关于音效 音效又称短音频,是一个声音文件,在应用程序中起到点缀效果,用于提升应用程序的整体用户体验.   我们手机里常见的APP几乎都少不了音效的点缀.   显示实现音效并不复杂,但对我们App很 ...