题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的。

所谓不稳婚姻是说。比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的老公虽说是M2.但她更爱M1,这种婚姻就是不稳婚姻,M1和F2理应结合,他们如今各自的婚姻都是错误的。

整个算法基于。男性轮流向女性求婚,每次求婚对象都是没有拒绝过自己且自己最喜欢的女性。而女性对于每一个求婚者。若她是单身,则接受。否则,就看她更喜欢当前求婚者还是她的未婚夫。选择更好的那个。

这样的运行过程之后。男的都会追到自己最喜欢的女性。而女性都会得到不会太差的男性= =,而且能够证明,这个婚姻是稳定的。。

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1000+10;
int pref[maxn][maxn],order[maxn][maxn],next[maxn];
int future_husband[maxn],future_wife[maxn];
queue<int> q;
void engage(int man,int woman)
{
int m=future_husband[woman];
if(m)
{
future_wife[m]=0;
q.push(m);
}
future_wife[man]=woman;
future_husband[woman]=man;
}
int id_man[555];
int id_woman[555];
char str[555];
int main()
{
int cas,n;
char A;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
vector<char> v;
for(int i=1;i<=n;i++) {cin>>A;id_man[A]=i;v.push_back(A);}
for(int i=1;i<=n;i++){cin>>A;id_woman[A]=i;}
for(int i=1;i<=n;i++)
{
scanf("%s",str);
int idm=id_man[str[0]];
for(int j=1;j<=n;j++)
{
int idw=id_woman[str[j+1]];
pref[idm][j]=idw;
}
next[idm]=1;
future_wife[idm]=0;
q.push(idm);
}
for(int i=1;i<=n;i++)
{
scanf("%s",str);
int idw=id_woman[str[0]];
for(int j=1;j<=n;j++)
{
int idm=id_man[str[j+1]];
order[idw][idm]=j;
}
future_husband[idw]=0;
} while(!q.empty())
{
int man=q.front();q.pop();
int woman=pref[man][next[man]++];
if(!future_husband[woman]) engage(man,woman);
else if(order[woman][man]<order[woman][future_husband[woman]]) engage(man,woman);
else q.push(man);
}
while(!q.empty()) q.pop();
for(int i=0;i<n;i++)
{
printf("%c %c\n",v[i],future_wife[v[i]-'a'+1]+'A'-1);
}
if(cas) puts("");
}
return 0;
}

谁告白谁占廉价,屌丝男快去告白吧。

poj 3478 The Stable Marriage Problem 稳定婚姻问题的更多相关文章

  1. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)

    The Stable Marriage Problem   Description The stable marriage problem consists of matching members o ...

  2. POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)

    Description The stable marriage problem consists of matching members of two different sets according ...

  3. [POJ 3487]The Stable Marriage Problem

    Description The stable marriage problem consists of matching members of two different sets according ...

  4. 【转】稳定婚姻问题(Stable Marriage Problem)

    转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...

  5. The Stable Marriage Problem

    经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...

  6. HDOJ 1914 The Stable Marriage Problem

    rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 6553 ...

  7. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

  8. 【HDOJ】1914 The Stable Marriage Problem

    稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...

  9. hdoj1435 Stable Match(稳定婚姻问题)

    简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...

随机推荐

  1. SGU 199 Beautiful People(DP+二分)

    时间限制:0.25s 空间限制:4M 题意: 有n个人,每个人有两个能力值,只有一个人的两个能力都小于另一个的能力值,这两个人才能共存,求能同时共存的最大人数. Solution: 显然这是一个两个关 ...

  2. SGU 224.Little Queens

    时间限制:0.75s 空间限制:6M 题意 n*n(n<=10)的棋盘,求出放置m(m<=n*n)个皇后的方案数. Solution: 状态压缩+位运算  搜索. 首先我们从上往下逐行放置 ...

  3. wordpress version

    version info /readme.html /wp-includes/version.php remove copyright 1.wp-login.php //<h1><a ...

  4. linux 监控系统缓存和cpu

    a=`free |head -n 2 |tail -n 1 |awk '{print $7}'`if [ $a -ge 900000 ];then     sync && echo 1 ...

  5. 4种检测是否支持HTML5的方法,你知道几个?

    4种检测是否支持HTML5的方法,你知道几个? 1,检查特定的属性是否存在于全局的对象里面,比如说window或navigator. 比如geolocation,它是HTML5新加支持的新特性:它是由 ...

  6. YII 小部件 解决多选按钮和单选按钮不在同一水平上 'separator'=>'&nbsp;'

    主要是添加separator属性(这里)$hoddy,$sex在控制器里面定义的数组,然后render传过来的 <td>          <?php echo $form-> ...

  7. Session和Cookie的学习笔记2

    <?php /* Cookie在计算机中保存的格式 用户名@网站地址[数字].txt Cookie在文件夹下,每个Cookie文件都是一个简单而又普通的文件件而不是程序,Cookie中的内容大多 ...

  8. 英语学习App演示

  9. IOS 点击空白处隐藏键盘的几种方法

    IOS 点击空白处隐藏键盘的几种方法     IOS7 点击空白处隐藏键盘的几种方法   IOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能 ...

  10. mysql oracle 删除外键约束

    mysql alter table xxx drop foreign key xxx cascade; oracle alter table drop constraint xxxxx cascade ...