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.
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
婚姻匹配算法:简单说就是让男生先选最优的,女生都不选,两个男生选同一个,女生在选一个更合适的,依次递推
算法介绍详见:https://blog.csdn.net/cscmaker/article/details/8291131
比较坑的地方是 poj 最后一个样例输出或不输出空格都能过 但 zoj 最后一个样例不能输出空格
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
typedef long long ll;
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define push_back pb
const int N=30+10;
int ml[N][N],wl[N][N];
int c1[N],c2[N];
int id1[N],id2[N];
int n;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
queue<int > q;
char name[2],str[30];
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",name);
id1[i]=name[0]-'a'+1;
q.push(id1[i]);
// cout<<id1[i]<<endl;
}
for(int i=1;i<=n;i++)
{
scanf("%s",name);
id2[i]=name[0]-'A'+1;
}
sort(id1+1,id1+1+n);
for(int i=1;i<=n;i++)
{
c1[i]=1;
scanf("%s",str);
for(int j=1;j<=n;j++)
ml[i][j]=str[j+1]-'A'+1;
// for(int j=1;j<=n;j++)
// cout<<ml[i][j]<<endl;
}
for(int i=1;i<=n;i++)
{
scanf("%s",str);
wl[i][0]=0;
for(int j=1;j<=n;j++)
wl[i][str[j+1]-'a'+1]=n+1-j;
// for(int j=1;j<=n;j++)
// cout<<wl[i][j]<<endl;
}
mem(c2,0);
while(!q.empty())
{
int now=q.front();
// cout<<now;
int wife=ml[now][c1[now]];
// cout<<" "<<wife<<" "<<c2[wife]<<endl;
if(wl[wife][now]>wl[wife][c2[wife]])
{
q.pop();
if(c2[wife])
{
q.push(c2[wife]);
c1[c2[wife]]++;
}
c2[wife]=now;
}
else c1[now]++;
}
for(int i=1;i<=n;i++)
printf("%c %c\n",id1[i]-1+'a',ml[id1[i]][c1[id1[i]]]-1+'A');
if(T)puts(""); // 坑点
}
return 0;
}