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
  1. 3
  2. Albert Laura Nancy Marcy
  3. Brad Marcy Nancy Laura
  4. Chuck Laura Marcy Nancy
  5. Laura Chuck Albert Brad
  6. Marcy Albert Chuck Brad
  7. Nancy Brad Albert Chuck
 
Sample Output
  1. Albert Nancy
  2. Brad Marcy
  3. Chuck Laura
 
Author
CHENG, Long
 
Source
ZOJ
 
Recommend
8600
 
  1. /*
  2. 题意:给你n个男生暗恋的对象,n个女生暗恋的对象,如果刚好能组成n对不重复的情侣,就输出,如果不可能的话,就输出Impossible
  3.  
  4. 初步思路:很典型的二分匹配问题
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. /***********************二分匹配模板**************************/
  9. const int MAXN=;
  10. int g[MAXN][MAXN];//编号是0~n-1的
  11. int linker[MAXN];//记录匹配点i的匹配点是谁
  12. bool used[MAXN];
  13. map<string,int> m;
  14. map<int ,string> M;
  15. int len=;
  16. int n;
  17. string bname,gname;
  18. bool dfs(int u)//回溯看能不能通过分手来进行匹配
  19. {
  20. int v;
  21. for(v=;v<n*;v++)
  22. if(g[u][v]&&!used[v])
  23. //如果有这条边,并且这条边没有用过
  24. {
  25. used[v]=true;
  26. if(linker[v]==-||dfs(linker[v]))//如果这个点没有匹配过,并且能找到匹配点,那么就可以以这个边作为匹配点
  27. {
  28. linker[v]=u;
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. int hungary()//返回最大匹配数
  35. {
  36. int res=;
  37. int u;
  38. memset(linker,-,sizeof(linker));
  39. for(u=;u<n*;u++)
  40. {
  41. memset(used,,sizeof(used));
  42. if(dfs(u))//如果这个点有匹配点
  43. res++;
  44. }
  45. return res;
  46. }
  47. /***********************二分匹配模板**************************/
  48. void init(){
  49. len=;
  50. memset(g,,sizeof g);
  51. }
  52. int main(){
  53. // freopen("in.txt","r",stdin);
  54. while(scanf("%d",&n)!=EOF){
  55. init();
  56. for(int i=;i<n;i++){
  57. cin>>bname;
  58. m[bname]=i;
  59. M[i]=bname;
  60. for(int j=;j<n;j++){
  61. cin>>gname;
  62. if(m.find(gname)==m.end()){
  63. m[gname]=++len;
  64. M[len]=gname;
  65. }
  66. g[m[bname]][m[gname]]=;
  67. }
  68. }
  69. // for(int i=0;i<n*2;i++){
  70. // cout<<M[i]<<" ";
  71. // }cout<<endl;
  72.  
  73. for(int i=;i<n;i++){
  74. cin>>gname;
  75. for(int j=;j<n;j++){
  76. cin>>bname;
  77. g[m[gname]][m[bname]]=;
  78. }
  79. }
  80. // for(int i=0;i<n*2;i++){
  81. // for(int j=0;j<n*2;j++){
  82. // cout<<g[i][j]<<" ";
  83. // }cout<<endl;
  84. // }
  85. //cout<<hungary()<<endl;
  86. if(hungary()==n*){
  87. for(int i=;i<n;i++){
  88. cout<<M[i]<<" "<<M[linker[i]]<<endl;
  89. }
  90. }else{
  91. puts("Impossible");
  92. }
  93. }
  94. return ;
  95. }

Marriage is Stable的更多相关文章

  1. HDU 1522 Marriage is Stable 稳定婚姻匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1522 #include<bits/stdc++.h> #define INF 0x3f3f3f3f ...

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

    <题目链接> 题目大意: 给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配. 解题分析: 稳定婚姻问题的 ...

  3. Marriage is Stable HDU1522 稳定婚姻问题基础

    几对男女   给出每个人心中的优先级   进行最合理的匹配 要打印名字的话必须有一个名字数组 英文名用map 稳定婚姻问题: 每次循环遍历所有的男的 每个男的对目前未被拒绝的并且优先级最高的进行预匹配 ...

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

    转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...

  5. acm数学(转)

    这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法    这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...

  6. [转] POJ数学问题

    转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  9. 【HDOJ图论题集】【转】

    =============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...

随机推荐

  1. C#的"?"修饰符和"??"运算符

    一.  ?  可空类型修饰符 “?”用来修饰为空的值类型,在加上“?”修饰符后,值类型也可以为空了,如: public int? CommandTimeout { get; }: var prop = ...

  2. APUE 4 - 线程

    对传统的UNIX进程来讲,一个进程中只有一个线程,这就意味着一个进程在同一时刻只能做一件事(即使是多核CPU).使用多线程技术, 我们可以设计程序使得一个进程在同一时刻做多件事.使用多线程编程具有以下 ...

  3. 一脸懵逼学习基于CentOs的Hadoop集群安装与配置

    1:Hadoop分布式计算平台是由Apache软件基金会开发的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS)和MapReduce(Google MapReduce的开源实现)为核心的 ...

  4. 关于如何获取iframe中的元素

    今天研究了一下iframe中元素的获取,发现有些地方还是有点坑的. 首先:如果使用纯前端手段,是没有办法获取非同源的iframe中的元素的,后面会提到后端手段 一.同源环境 1.首先在父页面获取ifr ...

  5. javascript中call()、apply()、bind()的用法终于理解

    其实是一个很简单的东西,认真看十分钟就从一脸懵B 到完全 理解! 先看明白下面: 例1 obj.objAge;  //17 obj.myFun()  //小张年龄undefined 例2 shows( ...

  6. JDownload: 一款可以从网络上下载文件的小程序第四篇(整体架构描述)

    一 前言 时间过得真快,距离本系列博客第一篇的发布已经过去9个月了,本文是该系列的第四篇博客,将对JDownload做一个整体的描述与介绍.恩,先让笔者把记忆拉回到2017年年初,那会笔者在看Unix ...

  7. python 设计模式,“多”例模式

    版本1:一个账号不能同时是司机乘客. #-*- coding:utf-8 -*- ''' Created on 2016年8月2日 @author: yangfanholiday ''' class ...

  8. cocos2dx - 伤害实现

    接上一节内容:cocos2dx - 生成怪物及AI 本节主要讲如何通过创建简单的矩形区域来造成伤害 在小游戏中简单的碰撞需求应用box2d等引擎会显得过于臃肿复杂,且功能不是根据需求定制,还要封装,为 ...

  9. Django 模型中自定义Manager和模型方法

    1.自定义管理器(Manager) 在语句Book.objects.all()中,objects是一个特殊的属性,通过它来查询数据库,它就是模型的一个Manager. 每个Django模型至少有一个m ...

  10. 用Python来实现列举某个文件夹内所有的文件列表

    用Python来实现列举某个文件夹内所有的文件列表.吾八哥我动手写代码之前分析了下,遍历一个文件夹,肯定是需要用到os模块了,查阅模块帮助信息,可知os.listdir()方法可以列举某个文件夹内的所 ...