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 (mf) 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.

 

题目大意:就是稳定婚姻问题,要求男士最优

思路:直接套用Gale-Shapley算法即可

PS:直接用数字不就好了吗非要用字符……

 

 1 #include <cstdio>
 2 #include <queue>
 3 #include <cstring>
 4 #include <map>
 5 using namespace std;
 6 
 7 const int MAXN = 100;
 8 
 9 int pref[MAXN][MAXN], order[MAXN][MAXN], next[MAXN];
10 int future_husband[MAXN], future_wife[MAXN];
11 queue<int> que;
12 map<char, int> mp;
13 
14 void engage(int man, int woman){
15     int &m = future_husband[woman];
16     if(m){
17         future_wife[m] = 0;
18         que.push(m);
19     }
20     future_husband[woman] = man;
21     future_wife[man] = woman;
22 }
23 
24 int n, T;
25 
26 void GaleShapley(){
27     while(!que.empty()){
28         int man = que.front(); que.pop();
29         int woman = pref[man][next[man]++];
30         if(!future_husband[woman] || order[woman][man] < order[woman][future_husband[woman]])
31             engage(man, woman);
32         else que.push(man);
33     }
34     for(char c = 'a'; c <= 'z'; ++c) if(mp[c])
35         printf("%c %c\n", c, future_wife[mp[c]] + 'A' - 1);
36 }
37 
38 int main(){
39     char s[MAXN], c[2];
40     scanf("%d", &T);
41     while(T--){
42         if(!que.empty()) que.pop();
43         mp.clear();
44         memset(pref,0,sizeof(pref));
45         memset(order,0,sizeof(order));
46         memset(future_husband,0,sizeof(future_husband));
47         memset(future_wife,0,sizeof(future_wife));
48         scanf("%d", &n);
49         for(int i = 1; i <= n; ++i) scanf("%s", c), mp[c[0]] = i;
50         for(int i = 1; i <= n; ++i) scanf("%s", c), mp[c[0]] = i;
51         for(int i = 0; i < n; ++i){
52             scanf("%s", s);
53             for(int j = 2; s[j]; ++j) pref[mp[s[0]]][j-1] = mp[s[j]];
54             next[mp[s[0]]] = 1;
55             que.push(mp[s[0]]);
56         }
57         for(int i = 0; i < n; ++i){
58             scanf("%s", s);
59             for(int j = 2; s[j]; ++j) order[mp[s[0]]][mp[s[j]]] = j-1;
60         }
61         GaleShapley();
62         if(T) printf("\n");
63     }
64 }
16MS

 

相关文章: