hdu 1522 Marriage is Stable(稳定婚姻问题)
来源:互联网 发布:网络大数据黑名单查询 编辑:程序博客网 时间:2024/05/23 23:53
Marriage is Stable
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1266 Accepted Submission(s): 667
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]).
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.
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.
Print a blank line after each test.
Sample Input
3Albert Laura Nancy MarcyBrad Marcy Nancy LauraChuck Laura Marcy NancyLaura Chuck Albert BradMarcy Albert Chuck BradNancy Brad Albert Chuck
Sample Output
Albert NancyBrad MarcyChuck Laura
Author
CHENG, Long
Source
ZOJ
Recommend
8600
题目大意:
有N男N女,每个男生有一个喜欢的女生排名,每个女生有一个喜欢的男生排名。让你找出一种结婚方式,使得每个人都结婚,并且不存在一对男女,每个人都喜欢对方超过喜欢当前的伴侣。
解题思路:
这个题意也就是稳定婚姻问题的由来。解法就直接用gale_shapley算法即可,详细说明摘自wiki。
简单来说就是先让男生贪心地找当前能找的最喜欢的女生,如果出现不稳定就更换为造成这个不稳定的更稳定的匹配。总时间复杂度O(N^2)。
AC代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <stack>#include <cmath>#include <cstdlib>#include <string>#include <map>#include <bitset>using namespace std;#define INF 0x3f3f3f3f#define LL long long#define fi first#define se second#define mem(a,b) memset((a),(b),sizeof(a))const int MAXN=500+3;int couple[MAXN];//每个女生匹配的男生queue<int> que;//存没有匹配的男生int man_like_woman_rank[MAXN][MAXN];//代表i男对女生的喜欢排名int woman_like_man_val[MAXN][MAXN];//代表i女对j男的喜欢程度int N;//每边的人数void gale_shapley(){ while(!que.empty()) que.pop(); for(int i=1;i<=N;++i) couple[i]=-1; for(int i=1;i<=N;++i) que.push(i); while(!que.empty()) { int man=que.front(); que.pop(); for(int i=1;i<=N;++i) if(man_like_woman_rank[man][i]!=-1) { //选择未被拒绝且最喜欢的女生 int woman=man_like_woman_rank[man][i]; man_like_woman_rank[man][i]=-1; int pre=couple[woman]; if(pre==-1)//对方没有匹配,直接匹配 { couple[woman]=man; break; } else { if(woman_like_man_val[woman][man]>woman_like_man_val[woman][pre])//更换匹配 { que.push(pre); couple[woman]=man; break; } } } }}string name[2][MAXN];//保存名字map<string, int> to_id[2];//名字映射到编号void init()//初始化{ to_id[0].clear(); to_id[1].clear();}int main(){ cin.sync_with_stdio(false); while(cin>>N) { init(); int cnt=0; string tmp; for(int i=1;i<=N;++i) { cin>>name[0][i]; int u=to_id[0][name[0][i]]=i; for(int j=1;j<=N;++j) { cin>>tmp; int v=to_id[1][tmp]; if(!v) { to_id[1][tmp]=v=++cnt; name[1][cnt]=tmp; } man_like_woman_rank[u][j]=v; } } for(int i=1;i<=N;++i) { cin>>tmp; int u=to_id[1][tmp]; for(int j=1;j<=N;++j) { cin>>tmp; int v=to_id[0][tmp]; woman_like_man_val[u][v]=N-j+1; } } gale_shapley(); bool ok=true; for(int i=1;i<=N;++i) if(couple[i]==-1)//有人没有匹配 { ok=false; break; } if(!ok) { cout<<"Impossible\n\n"<<endl; continue; } for(int i=1;i<=N;++i) cout<<name[0][couple[i]]<<' '<<name[1][i]<<'\n'; cout<<'\n'; } return 0;}
阅读全文
0 0
- hdu 1522 Marriage is Stable(稳定婚姻问题)
- hdu 1522Marriage is Stable(稳定婚姻问题)
- HDU 1522 Marriage is Stable 稳定婚姻问题
- HDU 1522 Marriage is Stable 稳定婚姻问题 (再水一发)
- 稳定婚姻问题 Poj 3487 The Stable Marriage Problem + Hdu 1522 Marriage is Stable (二分图稳定匹配)
- 【稳定婚姻问题】hdu1522 Marriage is Stable
- HDU 1522 Marriage is Stable 稳定婚姻匹配
- [HDU 1914] The Stable Marriage Problem (稳定婚姻问题)
- ZOJ 1576 Marriage is Stable 稳定婚姻问题
- ZOJ 1576 Marriage is Stable(稳定婚姻问题)
- HDU 1914 The Stable Marriage Problem 稳定婚姻问题
- Marriage is Stable (稳定婚姻匹配GS算法)
- POJ 3487 The Stable Marriage Problem (稳定婚姻问题)
- The Stable Marriage Problem(稳定婚姻问题)
- poj 3487 The Stable Marriage Problem(稳定婚姻问题)
- 稳定婚姻问题(The Stable Marriage Problem)
- HDU 1914 The Stable Marriage Problem 稳定婚姻问题 (接着水)
- HDU 1914 The Stable Marriage Problem 稳定婚姻
- 创建数学算法工具类事例
- ES6——进阶
- java中读取properties文件内容五种方式
- 队列-queue详解
- Miller_Rabin素性测试学习小结
- hdu 1522 Marriage is Stable(稳定婚姻问题)
- HDU5543 Pick The Sticks [背包dp变形]
- Oracle之where 和having中的区别-yellowcong
- 【Ambari】2.5.1.0.0汉化、构建和安装
- 什么是α测试?什么是β测试?
- vm桥接设置都正确时,如果还无法上网请关闭本机防火墙设置
- javascript面向对象——prototype属性(原型属性)
- Swoole笔记(五)
- Android双向表格可滑动可双向适配