【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> # ...
随机推荐
- Hadoop RPC机制的使用
一.RPC基础概念 1.1 RPC的基础概念 RPC,即Remote Procdure Call,中文名:远程过程调用: (1)它允许一台计算机程序远程调用另外一台计算机的子程序,而不用去关心底层的网 ...
- Blog Starting...
30出头,开始Blog记录学习生活的点滴,待40时再回来一看.
- 【转】Kylin中的cube构建
http://blog.csdn.net/yu616568/article/details/50365240 前言 在使用Kylin的时候,最重要的一步就是创建cube的模型定义,即指定度量和维度 ...
- css新增选择器
- winform里怎样在一个按钮上实现“单击”和“双击”事件?
Button按钮是没有双击事件(DoubleClick)的. button1.DoubleClick+=new EventHandler(button1_DoubleClick);使用这种方法在双击的 ...
- 一个android样本的过保护
前段时间处理一个android样本,样本本身作用不大,但是加了保护,遂做一个过保护的记录 通过dex2jar将dex转为jar文件的时候发现无法成功,通过抛出的异常可知,此处MainActivity: ...
- 6.android加密解析
编码.数字摘要.加密.解密 UrlEncoder /Urldecoder String str = "http://www.baidu.com?serach = 哈哈"; Stri ...
- Dapper ORM 用法—Net下无敌的ORM(转)
假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载Dapper的优势:1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编译后 ...
- ACdream1157 Segments(CDQ分治 + 线段树)
题目这么说的: 进行如下3种类型操作:1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]2)C i (1-base) 删除第i条增加的线段, ...
- XCOJ 1168 (搜索+期望+高斯消元法)
题目链接: http://xcacm.hfut.edu.cn/oj/problem.php?id=1168 题目大意:D是起点,E是终点.每次等概率往某个方向走,问到达终点的期望步数.到不了终点或步数 ...