HDOJ 1914 The Stable Marriage Problem
rt 稳定婚姻匹配问题
The Stable Marriage Problem
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 438 Accepted Submission(s): 222
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.
preferable lists for males. Next n lines describe preferable lists for females.
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
a A
b B
c C a B
b A
c C
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; int n;
char boy_name[30][2],girl_name[30][2];
int to_boy[26],to_girl[26]; int perfect_boy[30][30],perfect_girl[30][30];
int future_husband[30],future_wife[30];
int next[30];
queue<int> q; void init()
{
memset(boy_name,0,sizeof(boy_name));
memset(girl_name,0,sizeof(girl_name));
memset(to_boy,0,sizeof(to_boy));
memset(to_girl,0,sizeof(to_girl));
memset(perfect_boy,0,sizeof(perfect_boy));
memset(perfect_girl,0,sizeof(perfect_girl));
memset(future_husband,0,sizeof(future_husband));
memset(future_wife,0,sizeof(future_wife));
memset(next,0,sizeof(next));
while(!q.empty()) q.pop();
} void engage(int boy,int girl)
{
int m=future_husband[girl];
if(m)
{
future_wife[m]=0;
q.push(m);
}
future_husband[girl]=boy;
future_wife[boy]=girl;
} bool lover(int boy,int m,int girl)
{
for(int i=1;i<=n;i++)
{
if(perfect_boy[girl][i]==boy) return true;
if(perfect_boy[girl][i]==m) return false;
}
} int main()
{
int T_T,flag=0;
char in[50];
scanf("%d",&T_T);
while(T_T--)
{
if(flag==0) flag=1;
else putchar(10);
init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",boy_name[i]);
to_boy[boy_name[i][0]-'a']=i;
}
for(int i=1;i<=n;i++)
{
scanf("%s",girl_name[i]);
to_girl[girl_name[i][0]-'A']=i;
}
for(int i=0;i<n;i++)
{
scanf("%s",in);
int boy=to_boy[in[0]-'a'];
for(int j=2;j<n+2;j++)
{
int girl=to_girl[in[j]-'A'];
perfect_girl[boy][j-1]=girl;
}
q.push(i+1);
}
for(int i=0;i<n;i++)
{
scanf("%s",in);
int girl=to_girl[in[0]-'A'];
for(int j=2;j<n+2;j++)
{
int boy=to_boy[in[j]-'a'];
perfect_boy[girl][j-1]=boy;
}
}
while(!q.empty())
{
int boy=q.front(); q.pop();
int girl=perfect_girl[boy][++next[boy]];
int m=future_husband[girl];
if(m==0)
engage(boy,girl);
else
{
if(lover(boy,m,girl))
engage(boy,girl);
else q.push(boy);
}
}
for(int i=1;i<=n;i++)
{
int boy=to_boy[boy_name[i][0]-'a'];
printf("%c %c\n",boy_name[i][0],girl_name[future_wife[boy]][0]);
}
}
return 0;
}
HDOJ 1914 The Stable Marriage Problem的更多相关文章
- 【HDOJ】1914 The Stable Marriage Problem
稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...
- The Stable Marriage Problem
经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...
- 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage Problem Description The stable marriage problem consists of matching members o ...
- [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 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...
- poj 3478 The Stable Marriage Problem 稳定婚姻问题
题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的. 所谓不稳婚姻是说.比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的 ...
- 水题 HDOJ 4716 A Computer Graphics Problem
题目传送门 /* 水题:看见x是十的倍数就简单了 */ #include <cstdio> #include <iostream> #include <algorithm ...
随机推荐
- 黑马程序员——Objective-c特性
1. 继承 Objective-c不支持多继承. Super 关键字:调用该类的父类: 超类:父类的另一种说法. 2.自定义NSLog()输出: 在类中添加description方法就可以自定义NS ...
- java 抽象类和接口
接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...
- 理解KMP
KMP字符串模式匹配通俗点说就是一种在一个字符串中定位另一个串的高效算法.简单匹配算法的时间复杂度为O(m*n),KMP匹配算法,可以证明它的时间复杂度为O(m+n).. 一.简单匹配算法 先来看一个 ...
- WCF綁定
服务之间的通信方式是多种多样的,有多种可能的通信模式.包括:同步的请求与应答(Request/Reply)消息,或者异步的即发即弃(Fire-and-Forget)消息等等,在通信时传输的消息编码格式 ...
- Github在windows7环境下使用入门
1.下载并安装 下载和安装一般都没什么问题,网上的链接一大堆,不过还是在此给一个安装的地址和安装的参考吧. 当然,安装完成后要保证git能使用,必须配置github 2.配置github 首先是要创建 ...
- Linux网卡启动报错(this device is not active)
重启网络服务 service network restart 报如下错误: shutting down interface eth0: error:device "eth0" ...
- Myeclipse2014 自带的报表功能 与 Eclipse BIRT
Myeclipse2014 自带的报表功能跟 Eclipse BIRT 差不多,但不兼容 1.只能是MyEclipse Web projects 或者 Report Web project不支持B ...
- 设置TabBarItem选中时的图片及文字颜色
TabBarItem选中时,默认文字和图片都变为蓝色.使用以下代码可以进行修改. MainViewController *mainVC = [[MainViewController alloc] in ...
- Office 365 plans, SharePoint Online, and SharePoint 2013 on-premises 功能对比列表
Andrew ConnellSharePoint大牛整理了一个各个版本SharePoint功能对比列表,是SharePoint相关人员必备资料.赶紧收藏起来. SharePoint 2013 Feat ...
- 动态生成xml文件
使用xmlParser动态生成xml,输入的字符编码是gbk,结果怎么生成都不行,后来把输入转成utf8之后, 再生成就ok了