【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914
题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序,同时每位男生也按照自己的偏爱程度将女生排序。然后将这n个女生和n个男生配成完备婚姻。
如果存在两位女生A和B,两位男生a和b,使得A和a结婚,B和b结婚,但是A更偏爱b而不是a,b更偏爱A而不是B,则这个婚姻就是不稳定的,A和b可能背着别人相伴而走,因为他俩都认为,与当前配偶比起来他们更偏爱各自的新伴侣。
如果完备婚姻不是不稳定的,则称其是稳定的。通过证明,可以得到每一个n女n男的社团,都存在稳定婚姻的结论。但是这种情况只在异性的社团中存在。也就是说在同性的社团里面,稳定婚姻的存在性将不再被保证。
思路:先把所有男士加入队列当中,对于第一个出队列的男士从他喜爱度最高的女士开始求婚,如果找到一个女士还没有结婚,则和她匹配,如果找到一个女士,该女士对他的满意度高于这个女士的未婚夫,则该女士抛弃未婚夫和他进行匹配,她的未婚夫则进队列。已经匹配过的要进行标记,下次不能再匹配了。
因为每个男士最多和一个女士匹配一次。时间复杂度接近于O(n^2)。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include <queue>
#include <map>
using namespace std; const int maxn=;
int g[maxn][maxn], b[maxn][maxn], visit[maxn][maxn];
int bf[maxn], gf[maxn];
char ch[maxn], str[maxn];
map<char,int>G,M;
map<int,char>GG,MM;
queue<int>q;
int n, T; void init()
{
G.clear(), M.clear(), GG.clear(), MM.clear();
memset(visit,,sizeof(visit));
memset(bf,,sizeof(bf));
while(!q.empty()) q.pop();
} void find(int x)
{
for(int i=n; i>=; i--)
{
if(visit[x][i]) continue;
visit[x][i]=;
int y=b[x][i];
if(!bf[y])
{
bf[y]=x;
gf[x]=y;
return ;
}
else
{
if(g[y][x]>g[y][ bf[y] ])
{
q.push(bf[y]);
bf[y]=x;
gf[x]=y;
return ;
}
}
}
} void Solve()
{
for(int i=; i<=n; i++) q.push(i);
while(!q.empty())
{
int x=q.front();
q.pop();
find(x);
}
sort(ch+,ch+n+);
for(int i=; i<=n; i++)
printf("%c %c\n",ch[i],MM[ gf[ G[ch[i]] ] ]);
} int main()
{
cin >> T;
while(T--)
{
cin >> n;
init();
for(int i=; i<=n; i++) cin >> ch[i], G[ ch[i] ]=i, GG[i]=ch[i];
for(int i=; i<=n; i++) cin >> ch[n+i], M[ ch[n+i] ]=i, MM[i]=ch[n+i];
for(int i=; i<=n; i++)
{
scanf("%s",str+);
int x=G[ str[] ];
for(int j=; j<=n+; j++)
{
int y=M[ str[j] ];
b[x][n-j+]=y;
}
}
for(int i=; i<=n; i++)
{
scanf("%s",str+);
int x=M[ str[] ];
for(int j=; j<=n+; j++)
{
int y=G[ str[j] ];
g[x][y]=n-j+;
}
}
Solve();
if(T)puts("");
}
}
【HDU1914 The Stable Marriage Problem】稳定婚姻问题的更多相关文章
- 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage Problem Description The stable marriage problem consists of matching members o ...
- 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 ...
- 【转】稳定婚姻问题(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 ...
- [POJ 3487]The Stable Marriage Problem
Description The stable marriage problem consists of matching members of two different sets according ...
- 【HDOJ】1914 The Stable Marriage Problem
稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...
- hdoj1435 Stable Match(稳定婚姻问题)
简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...
随机推荐
- python logging模块 basicConfig配置文件
logging.basicConfig(level=log_level, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s ...
- [转载]explicit关键字
本文转自http://www.programlife.net/cpp-explicit-keyword.html. 其实explicit主要用于防止隐式转换,用于修饰构造函数.复制构造函数[注意:一般 ...
- nginx 引号 x22
这个好像是nginx故意这样做的. 因为Nginx默认的log_format使用双引号作为间隔符,为了避免日志分析时候出现混乱,所以将双引号解析为x22了. 只能每天日志切割的时候,自己替换日志中的x ...
- [bzoj3224]普通平衡树/3223文艺平衡树
这是一道很普通的题.. 最近花了很多时间来想要去干什么,感觉自己还是太拿衣服 做这道题是因为偶尔看到了lavender的blog和她的bzoj早期AC记录,就被题目深深地吸引到了,原因有二: 自己sp ...
- JavaScript 笔记 ( Prototype )
这阵子实在好忙 ( 这样说好像也不是一两个月了... ),然后因为工作伙伴都是 JavaScript 神之等级的工程师,从中也学到不少知识,毕竟就是要和强者工作才会成长呀!为了想好好瞭解他们写的程式码 ...
- MACD 基本用法
基本用法 1. MACD 金叉:DIFF 由下向上突破 DEA,为买入信号. 2. MACD 死叉:DIFF 由上向下突破 DEA,为卖出信号. 3. MACD 绿转红:MACD 值由负变正,市场由空 ...
- while的使用
我在用while的时候, while(当这个对象的首个字是汉字){ 执行语句 } 我发现出现死循环的现象 其实我要再添加一个条件,就是不管是不是上述条件都成立,最终还是要结束的.
- OA
OA(政府) http://zw.wuzhitong.com/oa_wuzhitong/index.asp http://www.wuzhitong.com/Information_ZW.html O ...
- BZOJ4665 : 小w的喜糖
考虑枚举哪些人一定不合法,那么方案数可以通过简单的排列组合算出. 于是设$f[i][j]$表示前$i$种糖果,一共有$j$个人一定不合法的方案数,但是这样并不能保证其他人一定合法,所以需要进行容斥. ...
- android presentation
对于双屏异显(lcd 和 hdmi 的双屏异显),android框架已经支持,但是底层接口功能还是要自己去实现,且需要底层驱动支持. 使用presentation 去画第二个display就好了. M ...