【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> # ...
随机推荐
- GCC安装
1.apt-get install gcc2.apt-get install make3.apt-get install gdb apt-get install build-essential 这个 ...
- (转)我所理解的OOP——UML六种关系
原文地址:http://www.cnblogs.com/dolphinX/p/3296681.html 最近由于经常给公司的小伙伴儿们讲一些OOP的基本东西,每次草纸都被我弄的很尴尬,画来画去自己都乱 ...
- Magento强大的配置系统
Magento的配置系统就像是Magento的心脏,支撑着Magento的运行.这套配置系统掌管着几乎所有"module/model/class/template/etc".它把整 ...
- Unity3D 之3D游戏SD快打 3D游戏基础入门开发全(1)
这里记录一个U3D游戏,3D游戏的基本开发. 导入素材 1.首先导入需要的素材.因为FBX格式的素材是通用的,所以尽量导入这样的资源使用 导入后的结果: 然后对人形骨骼进行设置. 看哪里没有映射到骨骼 ...
- Asp.Net Core简单整理
1.Asp.NetCore 中文入门文档 http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-index.html
- java 正则表达式匹配字符串
private static List<String> getImage(String str){ List<String> tmp=new ArrayList<Stri ...
- 学会用Reflector调试我们的MVC框架代码
我们知道,现在能调试.net程序通常有两个,第一个是ILSpy,还是一个是Reflector,这两个小反编译软件算是我们研究底层代码中所拥有的一把 锋利小尖刀~~~,比如你看到的ILSpy这样的界面图 ...
- iOS越狱系列(一):使用Reveal分析APP
TOOLS 1.已越狱的设备,并且已安装了OpenSSH,MobileSubstrate等实用工具 Cydia源/Telesphoreo里有 里面有个包 可以基本集合所有开发工具提供库 2.mac o ...
- Codevs 1814 最长链
1814 最长链 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少, ...
- bzoj1478:Sgu282 Isomorphism
思路:由于题目中是通过改变点的编号来判断两种染色方案是否相同,而染色的确是边,于是考虑如何将点置换转化为边置换. 对于一个有n个点的完全图,其点置换有n!个(即全排列个数),又由于每一个边置换都对应了 ...