Marriage is Stable

本文探讨了一种典型的二分匹配问题——稳定婚姻问题。通过给定的男女偏好列表,使用二分匹配算法来寻找一种稳定的匹配方案,确保不会有任意一对男女更倾向于彼此而非当前的伴侣。文章提供了一个C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Marriage is Stable

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 64 Accepted Submission(s): 43
 
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
ZOJ
 
Recommend
8600
 
/*
题意:给你n个男生暗恋的对象,n个女生暗恋的对象,如果刚好能组成n对不重复的情侣,就输出,如果不可能的话,就输出Impossible

初步思路:很典型的二分匹配问题
*/
#include<bits/stdc++.h>
using namespace std;
/***********************二分匹配模板**************************/
const int MAXN=1000;
int g[MAXN][MAXN];//编号是0~n-1的 
int linker[MAXN];//记录匹配点i的匹配点是谁
bool used[MAXN];
map<string,int> m;
map<int ,string> M;
int len=3;
int n;
string bname,gname;
bool dfs(int u)//回溯看能不能通过分手来进行匹配
{
    int v;
    for(v=0;v<n*2;v++)
        if(g[u][v]&&!used[v])
        //如果有这条边,并且这条边没有用过
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))//如果这个点没有匹配过,并且能找到匹配点,那么就可以以这个边作为匹配点
            {
                linker[v]=u;
                return true;
            }    
        }  
    return false;  
}    
int hungary()//返回最大匹配数
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<n*2;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u))//如果这个点有匹配点 
            res++;
    } 
    return res;   
}
/***********************二分匹配模板**************************/
void init(){
    len=2;
    memset(g,0,sizeof g);
}
int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF){
        init();
        for(int i=0;i<n;i++){
            cin>>bname;
            m[bname]=i;
            M[i]=bname;
            for(int j=0;j<n;j++){
                cin>>gname;
                if(m.find(gname)==m.end()){
                    m[gname]=++len;
                    M[len]=gname;
                }
                g[m[bname]][m[gname]]=1;
            }
        }
        // for(int i=0;i<n*2;i++){
            // cout<<M[i]<<" ";
        // }cout<<endl;
        
        for(int i=0;i<n;i++){
            cin>>gname;
            for(int j=0;j<n;j++){
                cin>>bname;
                g[m[gname]][m[bname]]=1;
            }
        }
        // for(int i=0;i<n*2;i++){
            // for(int j=0;j<n*2;j++){
                // cout<<g[i][j]<<" ";
            // }cout<<endl;
        // }
        //cout<<hungary()<<endl;
        if(hungary()==n*2){
            for(int i=0;i<n;i++){
                cout<<M[i]<<" "<<M[linker[i]]<<endl;
            }
        }else{
            puts("Impossible");
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/6421682.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值