hdu 1522 Marriage is Stable(稳定婚姻问题)

来源:互联网 发布:网络大数据黑名单查询 编辑:程序博客网 时间:2024/05/23 23:53

Marriage is Stable

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1266    Accepted Submission(s): 667
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 
 

Recommend
8600

题目大意:

    有N男N女,每个男生有一个喜欢的女生排名,每个女生有一个喜欢的男生排名。让你找出一种结婚方式,使得每个人都结婚,并且不存在一对男女,每个人都喜欢对方超过喜欢当前的伴侣。


解题思路:

    这个题意也就是稳定婚姻问题的由来。解法就直接用gale_shapley算法即可,详细说明摘自wiki。


    简单来说就是先让男生贪心地找当前能找的最喜欢的女生,如果出现不稳定就更换为造成这个不稳定的更稳定的匹配。总时间复杂度O(N^2)。


AC代码:

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <stack>#include <cmath>#include <cstdlib>#include <string>#include <map>#include <bitset>using namespace std;#define INF 0x3f3f3f3f#define LL long long#define fi first#define se second#define mem(a,b) memset((a),(b),sizeof(a))const int MAXN=500+3;int couple[MAXN];//每个女生匹配的男生queue<int> que;//存没有匹配的男生int man_like_woman_rank[MAXN][MAXN];//代表i男对女生的喜欢排名int woman_like_man_val[MAXN][MAXN];//代表i女对j男的喜欢程度int N;//每边的人数void gale_shapley(){    while(!que.empty())        que.pop();    for(int i=1;i<=N;++i)        couple[i]=-1;    for(int i=1;i<=N;++i)        que.push(i);    while(!que.empty())    {        int man=que.front(); que.pop();        for(int i=1;i<=N;++i)            if(man_like_woman_rank[man][i]!=-1)            {                //选择未被拒绝且最喜欢的女生                int woman=man_like_woman_rank[man][i];                man_like_woman_rank[man][i]=-1;                int pre=couple[woman];                if(pre==-1)//对方没有匹配,直接匹配                {                    couple[woman]=man;                    break;                }                else                {                    if(woman_like_man_val[woman][man]>woman_like_man_val[woman][pre])//更换匹配                    {                        que.push(pre);                        couple[woman]=man;                        break;                    }                }            }    }}string name[2][MAXN];//保存名字map<string, int> to_id[2];//名字映射到编号void init()//初始化{    to_id[0].clear();    to_id[1].clear();}int main(){    cin.sync_with_stdio(false);    while(cin>>N)    {        init();        int cnt=0;        string tmp;        for(int i=1;i<=N;++i)        {            cin>>name[0][i];            int u=to_id[0][name[0][i]]=i;            for(int j=1;j<=N;++j)            {                cin>>tmp;                int v=to_id[1][tmp];                if(!v)                {                    to_id[1][tmp]=v=++cnt;                    name[1][cnt]=tmp;                }                man_like_woman_rank[u][j]=v;            }        }        for(int i=1;i<=N;++i)        {            cin>>tmp;            int u=to_id[1][tmp];            for(int j=1;j<=N;++j)            {                cin>>tmp;                int v=to_id[0][tmp];                woman_like_man_val[u][v]=N-j+1;            }        }        gale_shapley();        bool ok=true;        for(int i=1;i<=N;++i)            if(couple[i]==-1)//有人没有匹配            {                ok=false;                break;            }        if(!ok)        {            cout<<"Impossible\n\n"<<endl;            continue;        }        for(int i=1;i<=N;++i)            cout<<name[0][couple[i]]<<' '<<name[1][i]<<'\n';        cout<<'\n';    }        return 0;}

阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 注销了的支付宝怎么办 狗狗黑色毛发红怎么办 蘑菇街直播间被禁言了怎么办 收了发票不付款怎么办 退款要先收发票怎么办 淘宝退款了又收到货怎么办 商家收货后拒绝退款怎么办 申请退货退款卖家不处理怎么办 淘宝买东西换货卖家不发货怎么办 淘宝自动默认付款没发货怎么办 支付宝支付失败可钱扣了怎么办 苹果nfc感应坏了怎么办 老鼠添过的盘子怎么办 ie浏览器页面显示网页错误怎么办 Ⅵvo手机声音小怎么办 小米手机预约错了怎么办 小米note二手没解锁怎么办 艾灸后脸色越黑怎么办 淘宝软件类目不能上架宝贝怎么办 ae中没有mpg格式怎么办 淘宝小二处理不公怎么办 遇到卖保险的人怎么办 租房合同没理家电清单怎么办 普雪油烟机坏了怎么办 我累了 真的累了怎么办 u盘15g变成4g了怎么办 属兔的买了东户怎么办 玩时时彩输了2万怎么办 胸变的又软又小怎么办 u盘16g变成4g了怎么办 1岁宝宝吃了就吐怎么办 脚崴了肿了很痛怎么办 九格拼图5在9那怎么办 4s锁屏密码忘了怎么办 6p触屏偶尔乱跳怎么办 新办劳务公司的劳务资质怎么办 汽车没电了打不着火怎么办 吃凉的甜的牙疼怎么办 学车对点对不上怎么办 发现老公有外遇最明智的怎么办 想开个童装店但是没经验怎么办