The Stable Marriage Problem
 

Description

The stable marriage problem consists of matching members of two different sets according to the member’s preferences for the other set’s members. The input for our problem consists of:

  • a set M of n males;
  • a set F of n females;
  • for each male and female we have a list of all the members of the opposite gender in order of preference (from the most preferable to the least).

A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (mf) such that f ∈ F prefers m ∈ M to her current partner and m prefers f over his current partner. The stable marriage A is called male-optimal if there is no other stable marriage B, where any male matches a female he prefers more than the one assigned in A.

Given preferable lists of males and females, you must find the male-optimal stable marriage.

Input

The first line gives you the number of tests. The first line of each test case contains integer n (0 < n < 27). Next line describes n male and n female names. Male name is a lowercase letter, female name is an upper-case letter. Then go n lines, that describe preferable lists for males. Next n lines describe preferable lists for females.

Output

For each test case find and print the pairs of the stable marriage, which is male-optimal. The pairs in each test case must be printed in lexicographical order of their male names as shown in sample output. Output an empty line between test cases.

Sample Input

2
3
a b c A B C
a:BAC
b:BAC
c:ACB
A:acb
B:bac
C:cab
3
a b c A B C
a:ABC
b:ABC
c:BCA
A:bac
B:acb
C:abc

Sample Output

a A
b B
c C a B
b A
c C

Source

 
 
【题意】
  自己看
 
【分析】
  怎么感觉稳定婚姻问题都没有其他问法。。。
  做的两题都是一样的。。。
  这题是male-optimal,就练练模版了。。。
 
代码如下:
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 40 int ml[Maxn][Maxn],rk[Maxn][Maxn];
int fw[Maxn],fh[Maxn],nt[Maxn]; int n;
// bool pm[Maxn],pf[Maxn];
char s[Maxn]; queue<int > q; void init()
{
scanf("%d",&n);
// memset(pm,0,sizeof(pm));
// memset(pf,,sizeof(pf));
for(int i=;i<=n;i++)
{
scanf("%s",s);
// pm[s[0]-'a'+1]=1;
}
for(int i=;i<=n;i++)
{
scanf("%s",s);
// pf[s[0]-'A'+1]=1;
}
while(!q.empty()) q.pop();
memset(fw,,sizeof(fw));
memset(fh,,sizeof(fh));
for(int i=;i<=n;i++)
{
scanf("%s",s);
int x=s[]-'a'+;
for(int j=;j<=n;j++)
{
int y=s[j+]-'A'+;
ml[x][j]=y;
}
fw[x]=;nt[x]=;
q.push(x);
}
for(int i=;i<=n;i++)
{
scanf("%s",s);
int x=s[]-'A'+;
for(int j=;j<=n;j++)
{
int y=s[j+]-'a'+;
rk[x][y]=j;
}
fh[x]=;
}
} void engage(int x,int y)
{
int z=fh[y];
if(z)
{
fw[z]=;
q.push(z);
}
fw[x]=y;fh[y]=x;
} void ffind()
{
while(!q.empty())
{
int x=q.front();q.pop();
int y=ml[x][nt[x]++];
if(fh[y]==||rk[y][x]<rk[y][fh[y]])
engage(x,y);
else q.push(x);
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
ffind();
for(int i=;i<=;i++) if(fw[i])
{
printf("%c %c\n",'a'+i-,'A'+fw[i]-);
}
if(T) printf("\n");
}
return ;
}

[POJ3487]

这样我还WA了两次,next不记得++,还有最好清零用memset全清了来得干净~~

2016-10-26 20:51:44

【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)的更多相关文章

  1. poj 3478 The Stable Marriage Problem 稳定婚姻问题

    题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的. 所谓不稳婚姻是说.比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的 ...

  2. POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)

    Description The stable marriage problem consists of matching members of two different sets according ...

  3. [POJ 3487]The Stable Marriage Problem

    Description The stable marriage problem consists of matching members of two different sets according ...

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

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

  5. The Stable Marriage Problem

    经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...

  6. HDOJ 1914 The Stable Marriage Problem

    rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 6553 ...

  7. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

  8. 【HDOJ】1914 The Stable Marriage Problem

    稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...

  9. hdoj1435 Stable Match(稳定婚姻问题)

    简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...

随机推荐

  1. Vivado学习笔记_002

    经过几天的试用逐渐熟悉了vivado,和ISE相比vivado确实有了很多改良. 发现了以下几个特点:   1.数据格式统一了 在以往的设计中,保存数据的格式非常多.ISE有很多种格式的文件,在tra ...

  2. ffmpeg之YUYV转RGB ARM使用流程分析

    本例基于3.2.2 ffmpeg 一.应用调用API 二.头文件包含的API接口 对应于libswscale.so.libswscale.so.4.libswscale.so.4.2.100中 sws ...

  3. MySQL之DML语句(insert update delete)

    DML主要针对数据库表对象的数据而言的,一般DML完成: 插入新数据 修改已添加的数据 删除不需要的数据 1.insert into插入语句 //主键自增可以不插入,所以用null代替 ); //指定 ...

  4. 【Java咬文嚼字】关键字(二):八个基本数据类型

    本次谈谈java中的基本数据类型.java中8个基本数据类型说多不多说少也不少,相对简单的记忆方式:整数型(byte,short,int,long).浮点型(float,double).逻辑型(boo ...

  5. 数据挖掘-关联规则分析[ZZ]

    1.什么是关联规则 "尿布与啤酒"的故事大家都有听过,这里就不罗嗦了. 按常规思维,尿布与啤酒风马牛不相及,若不是借助数据挖掘技术对大量交易数据进行挖掘分析,沃尔玛是不可能发现数据 ...

  6. 什么是CGI(Common Gateway Interface)?

    参考: 1.Python CGI编程 2.十分钟搞懂CGI 3.CGI Made Really Easy

  7. Jquery 动态生成表单 并将表单数据 批量通过Ajax插入到数据库

    利用Jquery 动态生成 Table 表单 之后利用each 方法来遍历所有文本框获取文本的value值  并通过Ajax 将数据 提交到Web服务里把数据插入数据库 Html页面 <!DOC ...

  8. eclipse汉化安装

    http://hi.baidu.com/rankabc/item/b07e03466550e4ce1381dac4 Eclipse汉化方法 网上搜了很多教程,步骤都不够详细,因此写一篇傻瓜版教程让新手 ...

  9. javascript 学习笔记之模块化编程

    题外: 进行web开发3年多了,javascript(后称js)用的也比较多,但是大部分都局限于函数的层次,有些公共的js函数可重用性不好,造成了程序的大量冗余,可读性差(虽然一直保留着注释的习惯,但 ...

  10. yii2 AR需要注意的地方

    $model::find(['id'=>1])->one();和$model::findOne(1); 返回的都是一个Obj不能使用foreach遍历,其他都是返回对象数组可以用forea ...