集训队专题(5.2)1004 Marriage is Stable
来源:互联网 发布:美术作品版权 淘宝 编辑:程序博客网 时间:2024/06/02 04:54
Marriage is Stable
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 845 Accepted Submission(s): 457
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
此题也是一个裸稳定婚姻问题,没有什么特别需要注意的地方,Gale Shapley算法一套即可。
#include <cstdio>#include <cstring>#include <string>#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<map>using namespace std;int n,gp_boy[505][505],gp_girl[505][505],boy[505],girl[505],rankl[505];map<string,int>mp_boy,mp_girl;string sboy[505],sgirl[505];char s[1000];void Gale_Shapley(){ memset(boy,0,sizeof(boy)); memset(girl,0,sizeof(girl)); for(int i=1; i<=n; i++) rankl[i]=1; while(1) { int flag=0; for(int i=1; i<=n; i++) { if(!boy[i]) { int g=gp_boy[i][rankl[i]++]; if(!girl[g]) boy[i] = g, girl[g] = i; else if(gp_girl[g][i]>gp_girl[g][girl[g]]) boy[girl[g]] = 0, girl[g] = i, boy[i] = g; flag = 1; } } if(!flag) break; } for(int i=1; i<=n; i++) { cout<<sboy[i]<<' '<<sgirl[boy[i]]<<endl; } puts("");}int main(){ while(~scanf("%d",&n)) { getchar(); mp_boy.clear(),mp_girl.clear(); int pos=1,tem; for(int i=1; i<=n; i++) { scanf("%s",s); sboy[i] = s,mp_boy[s] = i; for(int j=1; j<=n; j++) { scanf("%s",s); tem=mp_girl[s]; if(tem == 0) mp_girl[s] = tem = pos++;sgirl[tem] = s; gp_boy[i][j] = tem; } } for(int i=0; i<n; i++) { scanf("%s",s); int x=mp_girl[s]; for(int j=0; j<n; j++) { scanf("%s",s); int y=mp_boy[s]; gp_girl[x][y] = n-j; } } Gale_Shapley(); } return 0;}
1 0
- 集训队专题(5.2)1004 Marriage is Stable
- 集训队专题(5.2)1005 The Stable Marriage Problem
- 集训队专题(5.2)1002 Stable Match
- Marriage is Stable
- Marriage is Stable
- zoj 1576 Marriage is Stable
- HDOJ 1522 Marriage is Stable
- Marriage is Stable (稳定婚姻匹配GS算法)
- hdu 1522 Marriage is Stable(稳定婚姻问题)
- 集训队专题(4)1004 Where is the canteen
- 【稳定婚姻问题】hdu1522 Marriage is Stable
- stable marriage
- HDU 1522 Marriage is Stable 稳定婚姻问题 (再水一发)
- ZOJ 1576 Marriage is Stable 稳定婚姻问题
- hdu 1522Marriage is Stable(稳定婚姻问题)
- HDU 1522 Marriage is Stable 稳定婚姻问题
- HDU 1522 Marriage is Stable 稳定婚姻匹配
- H - Marriage is Stable 母函数模版题
- iOS开发篇(六)Masonry三方框架的使用与介绍
- JavaScript二级下拉框联动
- C语言调用shell命令方法
- 与GPU技术的第一次亲密接触
- 数据库中的某字段的值像(4,5,6,9,15),查询出包含字符"5"的记录,像(6,9,15)这样的不算
- 集训队专题(5.2)1004 Marriage is Stable
- iOS常用的持久化存储方式
- 字符串与ascii转换
- 三种去除ASCII字符串中重复字符的方法及耗时比较
- Java 静态代码分析工具
- Missing ios distribution signing identity xcode can request one for you
- 如何进行App推广以及广告推送
- CoreLocation的变化-iOS8
- php代码缓存问题