题目链接
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.
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
这道题与基本的稳定婚姻匹配问题稍微多了一个条件,就是需要去满足男生得到的是最优的这样的一个情况,那么也就是我们需要去保证,男生是先去选的,还是男孩纸最求女生问题,因为只有主动才是能获得自己的最优的女盆友的呀!
然后,剩下的就是稳定婚姻匹配的模板了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 27;
unordered_map<char, int> mp_b, mp_g;
int N, boy_love[maxN][maxN], girl_love[maxN][maxN];
char str_b[maxN], str_g[maxN];
char into[50];
int boy[maxN], girl[maxN], kth[maxN];
inline void G_S()
{
for(int i=1; i<=N; i++) boy[i] = girl[i] = kth[i] = 0;
bool flag = true;
while(true)
{
flag = false;
for(int i=1; i<=N; i++)
{
if(!boy[i])
{
int g = boy_love[i][++kth[i]];
if(!girl[g])
{
girl[g] = i;
boy[i] = g;
continue;
}
else if(girl_love[g][i] < girl_love[g][girl[g]])
{
boy[girl[g]] = 0;
girl[g] = i;
boy[i] = g;
}
flag = true;
}
}
if(!flag) break;
}
for(int i=1; i<=N; i++)
{
printf("%c %c\n", str_b[i], str_g[boy[i]]);
}
}
inline void init()
{
mp_b.clear();
mp_g.clear();
}
int main()
{
int T; scanf("%d", &T);
int Cas = 0;
while(T--)
{
if(Cas++ > 0) printf("\n");
scanf("%d", &N);
init();
for(int i=1; i<=N; i++)
{
scanf("%s", into);
str_b[i] = into[0];
mp_b[str_b[i]] = i;
}
for(int i=1; i<=N; i++)
{
scanf("%s", into);
str_g[i] = into[0];
mp_g[str_g[i]] = i;
}
for(int i=1; i<=N; i++)
{
scanf("%s", into);
int which_boy = mp_b[into[0]], len = (int)strlen(into);
for(int j=2; j<len; j++)
{
boy_love[which_boy][j-1] = mp_g[into[j]];
}
}
for(int i=1; i<=N; i++)
{
scanf("%s", into);
int which_girl = mp_g[into[0]], len = (int)strlen(into);
for(int j=2; j<len; j++)
{
girl_love[which_girl][mp_b[into[j]]] = j - 1;
}
}
G_S();
}
return 0;
}