【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice
Teenagers from the local high school have asked you to help them with the organization of next year’s
Prom. The idea is to find a suitable date for everyone in the class in a fair and civilized way. So,
they have organized a web site where all students, boys and girls, state their preferences among the
class members, by ordering all the possible candidates. Your mission is to keep everyone as happy as
possible. Assume that there are equal numbers of boys and girls.
Given a set of preferences, set up the blind dates such that there are no other two people of opposite
sex who would both rather have each other than their current partners. Since it was decided that the
Prom was Ladies’ Choice, we want to produce the best possible choice for the girls.Input
Input consists of multiple test cases the first line of the input contains the number of test cases.
There is a blank line before each dataset.
The input for each dataset consists of a positive integer N, not greater than 1,000, indicating the
number of couples in theclass. Next, there are N lines, each onecontaining the all the integers from 1
to N,ordered according to the girl’s preferences. Next, there are N lines, each one containing all the
integers from 1 to N, ordered according to the boy’s preferences.Output
The output for each dataset consists of a sequence of N lines, where the i-th line containsthe number
of the boy assigned to the i-th girl (from 1 to N).
Print a blank line between datasets.
Sample Input
1
5
1 2 3 5 4
5 2 4 3 1
3 5 1 2 4
3 4 2 1 5
4 5 1 2 3
2 5 4 1 3
3 2 4 1 5
1 2 4 3 5
4 1 2 5 3
5 3 2 4 1
Sample Output
1
2
5
3
4
【题意】
有n对男女,先给出每个女生对n位男生的选择意向,排在前面的优先选择,然后给出n位男生的选择意向,排在前面的优先选择.
输出每位女生的匹配,使得每位女生都是稳定的最佳选择.
【分析】
这是著名的稳定婚姻问题。
这题要求女士的最优选择,所以把男女反过来就好了。
2016-10-26 17:11:48
其实只要理解过程,代码是很好打的,来一个有解释的模版;
稳定婚姻问题:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1010 int ml[Maxn][Maxn],rk[Maxn][Maxn];
//ml[i][j]男士i心中排第j的女士是谁
//rk[i][j]女士i心中男士j排名第几
int fh[Maxn],fw[Maxn],nt[Maxn];
//fh[i]女士i的未婚夫 fw[i]男士i的未婚妻(若还没订婚则为0) nt[i]男士i下一次应该向谁求婚 queue<int > q; void engage(int x,int y) //让男士x和女士y订婚 并且解除女士y之前的婚约(若有)
{
int z=fh[y];
if(z)
{
fw[z]=;
q.push(z);
}
fw[x]=y;fh[y]=x;
} int n; void init()
{
scanf("%d",&n);
while(!q.empty()) q.pop();
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
scanf("%d",&ml[i][j]);
nt[i]=;
fw[i]=;
q.push(i);
} for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
int x;
scanf("%d",&x);
rk[i][x]=j;
}
fh[i]=;
}
} void ffind()
{
while(!q.empty())
{
int x=q.front();q.pop(); //未订婚男士求婚
int y=ml[x][nt[x]++];
if(fh[y]==||rk[y][x]<rk[y][fh[y]])
engage(x,y);
else q.push(x);//若求婚失败,则下次再来
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
ffind();
for(int i=;i<=n;i++) printf("%d\n",fw[i]);//输出的是男士可能结婚的女士中最优的一个
if(T) printf("\n");
}
return ;
}
稳定婚姻问题
【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)的更多相关文章
- LA 3989 - Ladies' Choice 稳定婚姻问题
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 3989 Ladies' Choice
Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
- UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法
题目链接:https://vjudge.net/problem/UVALive-3989 题解: 题意:有n个男生和n个女生.每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行. ...
- UVALive 3989 Ladies' Choice
经典的稳定婚姻匹配问题 UVALive - 3989 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: ...
- UVALive 3989Ladies' Choice(稳定婚姻问题)
题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...
- Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...
- 训练指南 UVALive - 3989(稳定婚姻问题)
ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...
随机推荐
- extjs6整合到web项目中
最近有一个项目需要应用extjs作为前端界面,因此研究了一下如何将extjs 6引入到项目中.以下是操作步骤 extjs6下载地址 extjs 6有gpl版本的,下载地址https://www.sen ...
- oracle数据库TNS
TNS是Oracle Net的一部分,专门用来管理和配置Oracle数据库和客户端连接的一个工具,在大多数情况下客户端和数据库要通讯,必须配置TNS,下面看一如何配置它吧: TNS简要介绍与应用 :O ...
- APP评价(星星点赞)很简单
1.用代码或者storyboard创建5个button(现在一般都是5个星星) 我用的是storyboard 记得一定要设置button的tag值 在.h中 @property (weak, nona ...
- struts 文件上传下载
上传 1.编写上传action类 UploadAction.java package jxf.b_upload; import java.io.File; import java.io.IOExcep ...
- UVA 10066 The Twin Towers(LCS)
Problem B The Twin Towers Input: standard input Output: standard output Once upon a time, in an anci ...
- poj2104:K-th Number
思路:可持久化线段树,利用权值线段树,把建树过程看成插入,插入第i个元素就在第i-1棵树的基础上新建结点然后得到第i棵树,那么询问区间[l,r]就是第r棵树上的信息对应减去第l-1棵树上的信息,然后再 ...
- Android中为窗口定义主题
在res/values/styles文件夹中定义如下: <style name="myTheme"> <item name="android:windo ...
- c++primer复习(六)—面向对象编程
1 C++中,通过基类的引用(或指针)调用虚函数时,发生动态绑定,两个条件(基类引用或指针.虚函数)缺一不可 虚函数的默认实参将发生静态绑定 2 继承层次的根类一般都需要定义虚析构函数 3 任意非st ...
- 九度OJ 1373 整数中1出现的次数(从1到n整数中1出现的次数)
题目地址:http://ac.jobdu.com/problem.php?pid=1373 题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU ...
- 基于ECharts 的地图例子
最近的一个项目要用到显示地图,本来用jq做了一个,但由于客户不满意(确实自己弄的样式效果都不是太理想),于是就上网搜了搜,最后决定基于百度的ECharts来弄地图 本来自己js基础不是很扎实,EC ...