Marriage is Stable
来源:互联网 发布:数据库时间怎么修改 编辑:程序博客网 时间:2024/06/07 12:02
http://acm.hdu.edu.cn/showproblem.php?pid=1522
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
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
#include<stdio.h>#include<string.h>#include<iostream>#include<map>using namespace std;#define NN 600int mm[NN][NN],gg[NN][NN],p[NN];int gg_match[NN],mm_match[NN];//gg_match[i]=j;表示第i个gg喜欢第j个mm;string ss[NN*2],str;map<string,int>mp_mm,mp_gg;int main(){int i,j,k,m,n,cnt,a,b;while(scanf("%d",&n)!=EOF){mp_gg.clear();mp_mm.clear();cnt=0;for(i=1;i<=n;i++){cin>>ss[i];mp_gg[ss[i]]=i;for(j=1;j<=n;j++){cin>>str;if(mp_mm[str]==0){mp_mm[str]=++cnt;ss[n+cnt]=str;}gg[i][j]=mp_mm[str];}}for(i=1;i<=n;i++){cin>>str;a=mp_mm[str];for(j=1;j<=n;j++){cin>>str;b=mp_gg[str];mm[a][b]=j;}}int flag=1;for(i=1;i<=n;i++){p[i]=1;mm_match[i]=gg_match[i]=-1;}while(flag){flag=0;for(i=1;i<=n;i++){if(gg_match[i]==-1&&p[i]<=n){a=gg[i][p[i]++];if(mm_match[a]==-1){mm_match[a]=i;gg_match[i]=a;}else if(mm[a][i]<mm[a][mm_match[a]]){gg_match[i]=a;gg_match[mm_match[a]]=-1;mm_match[a]=i;}flag=1;}}};for(i=1;i<=n;i++){j=gg_match[i];//printf("%d\n",j);cout<<ss[i]<<" "<<ss[j+n]<<endl;}cout<<"\n";}return 0;}
- Marriage is Stable
- Marriage is Stable
- zoj 1576 Marriage is Stable
- HDOJ 1522 Marriage is Stable
- 【稳定婚姻问题】hdu1522 Marriage is Stable
- stable marriage
- ZOJ 1576 Marriage is Stable 稳定婚姻问题
- hdu 1522Marriage is Stable(稳定婚姻问题)
- HDU 1522 Marriage is Stable 稳定婚姻问题
- 集训队专题(5.2)1004 Marriage is Stable
- HDU 1522 Marriage is Stable 稳定婚姻匹配
- H - Marriage is Stable 母函数模版题
- ZOJ 1576 Marriage is Stable(稳定婚姻问题)
- Marriage is Stable (稳定婚姻匹配GS算法)
- hdu 1522 Marriage is Stable(稳定婚姻问题)
- 稳定婚姻问题 Poj 3487 The Stable Marriage Problem + Hdu 1522 Marriage is Stable (二分图稳定匹配)
- Stable Marriage Question
- Stable Marriage Problem
- 转贴:Linux查看CPU个数/多核/多线程的查看
- resource
- ACM第一周第二周总结 2012-3-2
- 判断单链表是否有环
- UVa10220 - I Love Big Numbers !
- Marriage is Stable
- struts2 token拦截器防止表单重复提交
- 百度面试题:找出数组中出现次数超过一半的数
- 字符串反转,单词内容不变
- UVa10579 Fibonacci Numbers
- UVa10334 - Ray Through Glasses
- UVa495 - Fibonacci Freeze
- UVa10189 - Minesweeper
- 最长的滑道