The Stable Marriage Problem
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 360 Accepted Submission(s): 175
Problem Description
The stable marriage problem consists of matching members of two different sets according to the member’s preferences for the other set’s members. The input for our problem consists of:
a set M of n males;
a set F of n females;
for each male and female we have a list of all the members of the opposite gender in order of preference (from the most preferable to the least).
A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (m, f) such that f ∈ F prefers m ∈ M to her current partner and m prefers f over his current partner. The stable marriage A is called male-optimal if there is no other stable marriage B, where any male matches a female he prefers more than the one assigned in A.
Given preferable lists of males and females, you must find the male-optimal stable marriage.
a set M of n males;
a set F of n females;
for each male and female we have a list of all the members of the opposite gender in order of preference (from the most preferable to the least).
A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (m, f) such that f ∈ F prefers m ∈ M to her current partner and m prefers f over his current partner. The stable marriage A is called male-optimal if there is no other stable marriage B, where any male matches a female he prefers more than the one assigned in A.
Given preferable lists of males and females, you must find the male-optimal stable marriage.
Input
The first line gives you the number of tests. The first line of each test case contains integer n (0 < n < 27). Next line describes n male and n female names. Male name is a lowercase letter, female name is an upper-case letter. Then go n lines, that describe preferable lists for males. Next n lines describe preferable lists for females.
Output
For each test case find and print the pairs of the stable marriage, which is male-optimal. The pairs in each test case must be printed in lexicographical order of their male names as shown in sample output. Output an empty line between test cases.
Sample Input
2 3 a b c A B C a:BAC b:BAC c:ACB A:acb B:bac C:cab 3 a b c A B C a:ABC b:ABC c:BCA A:bac B:acb C:abc
Sample Output
a A b B c C a B b A c C
Source
Recommend
lcy
做了一天多终于做出来了,不是因为不知道思路,也不是因为没有稳定婚姻系列的模板,只是因为各种格式要求的限制和各种低级错误,哎,还是不行。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<queue>
#define M 1007
using namespace std;
int pref[M][M],order[M][M],next[M];
int future_husband[M],future_wife[M];
char s[M][M],z[M][M],ss[M],zz[M];
queue<int>q;
//订婚
void engage(int man,int women)
{
int m=future_husband[women];
if(m)//女士有现任丈夫m
{
future_wife[m]=0;//抛弃他
q.push(m);//m加入为订婚的男士队列
}
future_husband[women]=man;
future_wife[man]=women;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(future_husband,0,sizeof(future_husband));
memset(future_wife,0,sizeof(future_wife));
for(int i=1;i<=n;i++)
scanf("%s",ss);
for(int i=1;i<=n;i++)
scanf("%s",zz);
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]);
int j;
for(j=1;j<=n;j++)
{
pref[s[i][0]-'a'+1][j]=s[i][j+1]-'A'+1;//编号为i的男士第j个喜欢的人
}
next[s[i][0]-'a'+1]=1;//接下来应向排名为1的女士求婚
future_wife[s[i][0]-'a'+1]=0;//没有未婚妻
q.push(s[i][0]-'a'+1);
}
for(int i=1;i<=n;i++)
{
scanf("%s",z[i]);
for(int j=1;j<=n;j++)
{
order[z[i][0]-'A'+1][z[i][j+1]-'a'+1]=j;//在编号为i的女士心目中,编号为x的男士的排名
}
future_husband[z[i][0]-'A'+1]=0;//没有未婚夫
}
while(!q.empty())
{
int man=q.front();
q.pop();
int women=pref[man][next[man]++];//下一个求婚对象
if(!future_husband[women])//女士没有未婚夫,直接订婚
engage(man,women);
else if(order[women][man]<order[women][future_husband[women]])//代替女士的现任丈夫
engage(man,women);
else q.push(man);//直接拒绝,下次再来
}
while(!q.empty())q.pop();
for(int i=1;i<=n;i++)
printf("%c %c\n",i-1+'a',future_wife[i]-1+'A');
if(t)
printf("\n");
}
return 0;
}