poj3487-The Stable Marriage Problem

来源:互联网 发布:淘宝客活动报名审核 编辑:程序博客网 时间:2024/05/22 10:32

poj3487-The Stable Marriage Problem

The Stable Marriage ProblemTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 3084Accepted: 1311DescriptionThe 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.InputThe 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.OutputFor 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 Input23a b c A B Ca:BACb:BACc:ACBA:acbB:bacC:cab3a b c A B Ca:ABCb:ABCc:BCAA:bacB:acbC:abcSample Outputa Ab Bc Ca Bb Ac CSourceSoutheastern Europe 2007

稳定婚姻问题。
谁告白谁在婚姻中占主导地位。
试想,{男生从最喜欢的女生开始告白,直到有一个女生接受他},比{他等待着女生向自己告白,只能选择其中自己最喜欢的一个}男生在婚姻中占优势。

延迟认可算法 流程:
(1)初始化所有男生被拒绝
(2)当还有男生没被考虑
———(2.1)每个男生从自己最喜欢的女生开始找一个没有拒绝自己的女生告白
———(2.2)女生从向自己告白的男生里选择一个自己最喜欢的男生,对他延迟考虑(作为备胎??),并拒绝其他向自己告白的男生(我们这些屌丝)。可以在(2.1)的过程维护(2.2)。

结论:我们是否应该主动一点?

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 200;int n;char boyName[MAXN][MAXN], girlName[MAXN][MAXN], s[MAXN];int power[MAXN][MAXN], boyWant[MAXN][MAXN], girlWant[MAXN][MAXN], nowSheHas[MAXN], boy[MAXN], girl[MAXN];bool refuse[MAXN][MAXN];void solve(){    memset(refuse, 0, sizeof refuse);    memset(nowSheHas, 0, sizeof nowSheHas);    int flag1 = 1;    while (flag1) {      flag1 = 0;      for (int i = 1; i <= n; ++i)        for (int j = 1; j <= n; ++j) {          int by = boy[i];          int gl = boyWant[by][j];          if (nowSheHas[gl] == by) break;          if (!refuse[by][gl]) {            if (power[gl][nowSheHas[gl]] > power[gl][by])              refuse[by][gl] = 1, flag1 = 1;            else{              if (nowSheHas[gl]) flag1 = 1;              refuse[nowSheHas[gl]][gl] = 1;              nowSheHas[gl] = by;            }            break;          }        }    }    for (int i = 1; i <= n; ++i)      for (int j = 1; j <= n; ++j)        if (nowSheHas[girl[j]] == boy[i])          printf("%c %c\n", boy[i]+'a'-1, girl[j]+'A'-1);    printf("\n");}int main(){    int T;    scanf("%d", &T);    while (T--) {      scanf("%d", &n);      for (int i = 1; i <= n; ++i) {        scanf("%s", boyName[i]);        boy[i] = boyName[i][0] - 'a' + 1;      }      for (int i = 1; i <= n; ++i) {        scanf("%s", girlName[i]);        girl[i] = girlName[i][0] - 'A' + 1;      }      for (int i = 1; i <= n; ++i) {        scanf("%s", s);        int tot = 0, flag = 0;        for (int j = 0; j < strlen(s); ++j) {          if (s[j] == ':' && !flag) flag = 1;          else if (flag) boyWant[boy[i]][++tot] = s[j] - 'A' + 1;        }      }      for (int i = 1; i <= n; ++i) {        scanf("%s", s);        int tot = 0, flag = 0;        for (int j = 0; j < strlen(s); ++j) {          if (s[j] == ':' && !flag) flag = 1;          else if (flag) girlWant[girl[i]][++tot] = s[j] - 'a' + 1;        }        for (int j = 1; j <= n; ++j)          power[girl[i]][girlWant[girl[i]][j]] = n - j + 1;      }      solve();    }    return 0;}
阅读全文
0 0