【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage ProblemDescription
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 (m, f) 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:abcSample Output
a A
b B
c C a B
b A
c CSource
#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 (稳定婚姻问题)的更多相关文章
- poj 3478 The Stable Marriage Problem 稳定婚姻问题
题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的. 所谓不稳婚姻是说.比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的 ...
- POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)
Description The stable marriage problem consists of matching members of two different sets according ...
- [POJ 3487]The Stable Marriage Problem
Description The stable marriage problem consists of matching members of two different sets according ...
- 【转】稳定婚姻问题(Stable Marriage Problem)
转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...
- The Stable Marriage Problem
经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...
- HDOJ 1914 The Stable Marriage Problem
rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 6553 ...
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...
- 【HDOJ】1914 The Stable Marriage Problem
稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...
- hdoj1435 Stable Match(稳定婚姻问题)
简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...
随机推荐
- 微信公众号支付(三):页面调用微信支付JS并完成支付
一.调用微信的JS文件 1.首先要绑定[JS接口安全域名],“公众号设置”的“功能设置”中 2.引入JS文件 备注:支持使用 AMD/CMD 标准模块加载方法加载 <script type=&q ...
- 2013年10月13日学习:SQL通过命令语句来创建表
优点:操作简单,不容易出错,易于调试 缺点:需要记住命令.命令多了就容易混淆,是吧!但是熟悉了时间长了就OK了! step 1. 新建数据库,命名为Test 点击图形化界面中的新建查询,此时就可以输入 ...
- SQL Server调优系列基础篇 - 性能调优介绍
前言 关于SQL Server调优系列是一个庞大的内容体系,非一言两语能够分析清楚,本篇先就在SQL 调优中所最常用的查询计划进行解析,力图做好基础的掌握,夯实基本功!而后再谈谈整体的语句调优. 通过 ...
- Java NIO 选择器(Selector)的内部实现(poll epoll)
http://blog.csdn.net/hsuxu/article/details/9876983 之前强调这么多关于linux内核的poll及epoll,无非是想让大家先有个认识: Java NI ...
- Java Web应用启动间隔执行的程序
Reference:<Java定时器timer.schedule在Web中间隔执行任务和定时><[Java]Timer和TimerTask详解> 做了一个Demo,完成如下的功 ...
- C#&JQ仿网上商城商品条件筛选功能
1.后台绑定: 一种案例: 根据第一级显示第二级,并带有每个二级的“全部”功能: #region 绑定区域 #region 绑定一级区域 ) <= ? : (PageIndex - )) + , ...
- C# Flash 图片上传案例(结合网上腾讯头像上传Flash插件)
之前遇到过很多次要上传类似头像图片这种功能需求,这次是要求弄一个flash插件上传图片 感谢主,一个偶然机会在网上找到了一个很好的腾讯头像修改的flash插件:插件下载 这个功能采用Ajax访问支持, ...
- 直播类送礼动画<豪华礼物+小礼物>
直播类送礼动画<豪华礼物+小礼物>:代码会持续更新,现直播的app里内有太多的动画,由于时间关系不能一次共享所有动画聘为,这次先共享几个比较火爆的动画. 支持真机和模拟器上运行,最低支持i ...
- UVA 11991 Easy Problem from Rujia Liu?(vector map)
Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...
- Universal Naming Convention (UNC)
Quote from: http://compnetworking.about.com/od/windowsnetworking/g/unc-name.htm Definition: UNC is a ...