集训队专题(5.2)1004 Marriage is Stable

来源:互联网 发布:美术作品版权 淘宝 编辑:程序博客网 时间:2024/06/02 04:54

Marriage is Stable

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 845    Accepted Submission(s): 457
Special Judge


Problem Description
Albert, Brad, Chuck are happy bachelors who are in love with Laura, Marcy, Nancy. They all have three choices. But in fact, they do have some preference in mind. Say Albert, he likes Laura best, but that doesn't necesarily mean Laura likes him. Laura likes Chuck more than Albert. So if Albert can't marry Laura, he thinks Nancy a sensible choice. For Albert, he orders the girls Laura > Nancy > Marcy.

For the boys:

Albert: Laura > Nancy > Marcy
Brad: Marcy > Nancy > Laura
Chuck: Laura > Marcy > Nancy

For the girls:

Laura: Chuck > Albert > Brad
Marcy: Albert > Chuck > Brad
Nancy: Brad > Albert > Chuck

But if they were matched randomly, such as

Albert <-> Laura 
Brad <-> Marcy
Chuck <-> Nancy

they would soon discover it's not a nice solution. For Laura, she likes Chuck instead of Albert. And what's more, Chuck likes Laura better than Nancy. So Laura and Chuck are likely to come together, leaving poor Albert and Nancy.

Now it's your turn to find a stable marriage. A stable marriage means for any boy G and girl M, with their choice m[G] and m[M], it will not happen that rank(G, M) < rank(G, m[G])and rank(M, G) < rank(M, m[M]).
 

Input
Each case starts with an integer n (1 <= n <= 500), the number of matches to make.

The following n lines contain n + 1 names each, the first being name of the boy, and rest being the rank of the girls.

The following n lines are the same information for the girls.

Process to the end of file.
 

Output
If there is a stable marriage, print n lines with two names on each line. You can choose any one if there are multiple solution. Print "Impossible" otherwise.

Print a blank line after each test.
 

Sample Input
3Albert Laura Nancy MarcyBrad Marcy Nancy LauraChuck Laura Marcy NancyLaura Chuck Albert BradMarcy Albert Chuck BradNancy Brad Albert Chuck
 

Sample Output
Albert NancyBrad MarcyChuck Laura
 

Author
CHENG, Long
 

Source
ZOJ
 

此题也是一个裸稳定婚姻问题,没有什么特别需要注意的地方,Gale Shapley算法一套即可。

#include <cstdio>#include <cstring>#include <string>#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<map>using namespace std;int n,gp_boy[505][505],gp_girl[505][505],boy[505],girl[505],rankl[505];map<string,int>mp_boy,mp_girl;string sboy[505],sgirl[505];char s[1000];void Gale_Shapley(){    memset(boy,0,sizeof(boy));    memset(girl,0,sizeof(girl));    for(int i=1; i<=n; i++) rankl[i]=1;    while(1)    {        int flag=0;        for(int i=1; i<=n; i++)        {            if(!boy[i])            {                int g=gp_boy[i][rankl[i]++];                if(!girl[g])                    boy[i] = g, girl[g] = i;                else if(gp_girl[g][i]>gp_girl[g][girl[g]])                    boy[girl[g]] = 0, girl[g] = i, boy[i] = g;                flag = 1;            }        }        if(!flag) break;    }    for(int i=1; i<=n; i++)    {        cout<<sboy[i]<<' '<<sgirl[boy[i]]<<endl;    }    puts("");}int main(){    while(~scanf("%d",&n))    {        getchar();        mp_boy.clear(),mp_girl.clear();        int pos=1,tem;        for(int i=1; i<=n; i++)        {            scanf("%s",s);            sboy[i] = s,mp_boy[s] = i;            for(int j=1; j<=n; j++)            {                scanf("%s",s);                tem=mp_girl[s];                if(tem == 0)                mp_girl[s] = tem = pos++;sgirl[tem] = s;                gp_boy[i][j] = tem;            }        }        for(int i=0; i<n; i++)        {            scanf("%s",s);            int x=mp_girl[s];            for(int j=0; j<n; j++)            {                scanf("%s",s);                int y=mp_boy[s];                gp_girl[x][y] = n-j;            }        }        Gale_Shapley();    }    return 0;}


1 0
原创粉丝点击