https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1990

题目大意:

在盛大的校园舞会上有n位男生和n位女生,每人都对每个异性有一个排序,代表对他们的喜欢程度。你的任务是将男生和女生一一配对,使得男生U和女生V不存在一下情况1.男生u和女生v不是舞伴2,他们喜欢对方的程度都大于各自当前舞伴的程度。如果出现了2中的情况,他们可能擅自抛下自己的舞伴,另外组成一对。

你的任务是对于每个女生,在所有可能和她跳舞的男生中,找出她最喜欢的那一个。

思路:

看图。。。。

刘汝佳书上的原题。。划线的部分我只是想表明这个算法的。。。。丧失。。。。

稳定婚姻问题,求婚拒绝算法的实现。。。我觉得我也丧心病狂了。。。。。。情人节就是要做这种。。。。。

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN=1000+10;
int future_wife[MAXN],future_husband[MAXN];
int order_man[MAXN][MAXN],order_woman[MAXN][MAXN],next_persue[MAXN];
queue<int> q; //为订婚男士排队中。。。
void engage(int man,int woman) //订婚
{
int getout=future_husband[woman]; //这个可怜的男人被抛弃了。当然可能为0,就是没有这个人
if(getout!=0)
{
q.push(getout); //兄弟,你得重新找别人了。。
future_wife[getout]=0; //抛弃现任未婚夫
}
future_husband[woman]=man;
future_wife[man]=woman;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
while(!q.empty())
q.pop(); int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
scanf("%d",&order_man[i][j]); //编号为i的男士第j个喜欢的人。
future_wife[i]=0; //接下来应该向排名为1的女士求婚。
next_persue[i]=1; //没有未婚妻。
q.push(i);
} for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
int temp;
scanf("%d",&temp);
order_woman[i][temp]=j; //在编号为i的女士心目中,编号为temp的男士排名
}
future_husband[i]=0; //没有未婚夫
}
while(!q.empty())
{
int man=q.front(); //该这个男的求婚了!
q.pop();
int woman=order_man[man][next_persue[man]++]; //下一个求婚对象
if(future_husband[woman]==0) //如果女士没有对象,直接订婚
engage(man,woman);
else if(order_woman[woman][man]<order_woman[woman][ future_husband[woman] ])
engage(man,woman); //由于好感度高于现在女士的未婚夫,所以抢亲成功。
else
q.push(man); //直接被拒绝,下次再来。
}
for(int i=1;i<=n;i++) printf("%d\n",future_wife[i]);
if(T) printf("\n");
}
return 0;
}

LA 3989 - Ladies' Choice 稳定婚姻问题的更多相关文章

  1. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  2. UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法

    题目链接:https://vjudge.net/problem/UVALive-3989 题解: 题意:有n个男生和n个女生.每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行. ...

  3. UVALive 3989 Ladies' Choice

    Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...

  4. UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)

    题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...

  5. UVALive 3989Ladies' Choice(稳定婚姻问题)

    题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...

  6. UVALive 3989 Ladies&#39; Choice

    经典的稳定婚姻匹配问题 UVALive - 3989 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format:  ...

  7. Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法

    /** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...

  8. 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)

    Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...

  9. UVALive-3989 Ladies' Choice (稳定婚姻问题)

    题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...

随机推荐

  1. [Python] Generates permutations

    >>> import itertools >>> for p in itertools.permutations('ABCD'): ... print(p) ('A ...

  2. 把文件保存到 sdcard

    直接上代码: package com.example.test; import java.io.File; import java.io.FileNotFoundException; import j ...

  3. vim-进入插入模式快捷键

    vim中有一些命令,是同时包含有大小写两种的.现在就集中测试下他们的区别:     1.A 跟a A-光标所在行的末尾插入 a-光标后插入 2.I 跟i I-光标所在行的非空字符前插入 i-光标前位置 ...

  4. 67.nodejs取参四种方法req.body,req.params,req.param,req.body

    转自:http://www.cnblogs.com/jkingdom/p/8065202.html 摘要: nodejs取参四种方法req.body,req.params,req.param,req. ...

  5. POJ 1988 带偏移量的并查集

    题意: 思路: 数据范围很大 貌似只能用并查集了-- //By SiriusRen #include <cstdio> using namespace std; int p,f[33333 ...

  6. 荣获CCF(中国计算机学会)高级会员代表资格

    详细地址:http://www.ccf.org.cn/sites/ccf/xjhydb.jsp?contentId=2624287722908 650) this.width=650;" b ...

  7. UVA 12075 Counting Triangles

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. Java:异常体系

    异常的类别:可处理异常,运行时异常,非运行时异常 子类重写父类方法,父类方法有异常抛出, 子类重写父类的方法? 不能比父类抛出更大的异常 前言:java 中的异常处理机制你真的理解了吗?掌握了吗?ca ...

  9. C++集合运算函数总结 & 需要有序集合的操作

    前提:两个集合已经有序.merge() //归并两个序列,元素总个数不变,只是将两个有序序列归并为一个有序序列.set_union() //实现求集合A,B的并.set_difference()//实 ...

  10. ORA-01031: 权限不足

    1.错误描写叙述 ORA-01031: 权限不足 2.错误原因 SQL> create user yhd identified by scott account unlock; create u ...