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

87 篇文章 0 订阅

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
  
  
3 Albert Laura Nancy Marcy Brad Marcy Nancy Laura Chuck Laura Marcy Nancy Laura Chuck Albert Brad Marcy Albert Chuck Brad Nancy Brad Albert Chuck
 

Sample Output
  
  
Albert Nancy Brad Marcy Chuck Laura
 

Author
CHENG, Long
 

Source
 

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值