Marriage is Stable

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;
}
时间: 2024-11-03 22:11:11

Marriage is Stable的相关文章

HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)

<题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的模板题,需要用到Gale_Shapley算法,GS算法讲解  >>> 这个算法还是很直观的. 1 #include <iostream> 2 #include <cstring> 3 #include <stack> 4 #include <stri

Marriage is Stable HDU1522 稳定婚姻问题

几对男女   给出每个人心中的优先级   进行最合理的匹配 要打印名字的话必须有一个名字数组 英文名用map 稳定婚姻问题: 每次循环遍历所有的男的 每个男的对目前未被拒绝的并且优先级最高的进行预匹配  如果1女的没有伴侣2女的对该男的好感度比女的伴侣的好感度更高   满足任意一个则进行匹配 不断循环直到所有男的都有伴侣 #include<cstdio> #include<string> #include<map> #include<iostream> #i

【转】稳定婚姻问题(Stable Marriage Problem)

转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序,同时每位男生也按照自己的偏爱程度将女生排序.然后将这n个女生和n个男生配成完备婚姻. 如果存在两位女生A和B,两位男生a和b,使得A和a结婚,B和b结婚,但是A更偏爱b而不是a,b更偏爱A而不是B,则这个婚姻就是不稳定的,A和b可能背着别人相

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

[转] POJ数学问题

转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能完全看懂了,理解了再去做题,不要只记个公式. *简单题:(直接用套公式就可以了) pku2409 Let it Bead      http://acm.pku.edu.cn/JudgeOnline/problem?id=2409 pku2154 Co

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?