HDOJ 1914 The Stable Marriage Problem

來源:互聯網
上載者:User

標籤:des   style   http   java   color   get   


rt 穩定婚姻匹配問題

The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 438    Accepted Submission(s): 222


Problem DescriptionThe 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 Input
23a b c A B Ca:BACb:BACc:ACBA:acbB:bacC:cab3a b c A B Ca:ABCb:ABCc:BCAA:bacB:acbC:abc
 
Sample Output
a Ab Bc Ca Bb Ac C
 
SourceSoutheastern Europe 2007 


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int n;char boy_name[30][2],girl_name[30][2];int to_boy[26],to_girl[26];int perfect_boy[30][30],perfect_girl[30][30];int future_husband[30],future_wife[30];int next[30];queue<int> q;void init(){    memset(boy_name,0,sizeof(boy_name));    memset(girl_name,0,sizeof(girl_name));    memset(to_boy,0,sizeof(to_boy));    memset(to_girl,0,sizeof(to_girl));    memset(perfect_boy,0,sizeof(perfect_boy));    memset(perfect_girl,0,sizeof(perfect_girl));    memset(future_husband,0,sizeof(future_husband));    memset(future_wife,0,sizeof(future_wife));    memset(next,0,sizeof(next));    while(!q.empty()) q.pop();}void engage(int boy,int girl){    int m=future_husband[girl];    if(m)    {        future_wife[m]=0;        q.push(m);    }    future_husband[girl]=boy;    future_wife[boy]=girl;}bool lover(int boy,int m,int girl){    for(int i=1;i<=n;i++)    {        if(perfect_boy[girl][i]==boy) return true;        if(perfect_boy[girl][i]==m) return false;    }}int main(){    int T_T,flag=0;    char in[50];    scanf("%d",&T_T);    while(T_T--)    {        if(flag==0) flag=1;        else putchar(10);        init();        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            scanf("%s",boy_name[i]);            to_boy[boy_name[i][0]-'a']=i;        }        for(int i=1;i<=n;i++)        {            scanf("%s",girl_name[i]);            to_girl[girl_name[i][0]-'A']=i;        }        for(int i=0;i<n;i++)        {            scanf("%s",in);            int boy=to_boy[in[0]-'a'];            for(int j=2;j<n+2;j++)            {                int girl=to_girl[in[j]-'A'];                perfect_girl[boy][j-1]=girl;            }            q.push(i+1);        }        for(int i=0;i<n;i++)        {            scanf("%s",in);            int girl=to_girl[in[0]-'A'];            for(int j=2;j<n+2;j++)            {                int boy=to_boy[in[j]-'a'];                perfect_boy[girl][j-1]=boy;            }        }        while(!q.empty())        {            int boy=q.front(); q.pop();            int girl=perfect_girl[boy][++next[boy]];            int m=future_husband[girl];            if(m==0)                engage(boy,girl);            else            {                if(lover(boy,m,girl))                    engage(boy,girl);                else q.push(boy);            }        }        for(int i=1;i<=n;i++)        {            int boy=to_boy[boy_name[i][0]-'a'];            printf("%c %c\n",boy_name[i][0],girl_name[future_wife[boy]][0]);        }    }    return 0;}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.