【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 ...
随机推荐
- Java 字节数组类型(byte[])与int类型互转
代码如下: public class CommonUtils { //高位在前,低位在后 public static byte[] int2bytes(int num){ byte[] result ...
- 整理SVN代码-->正式环境的代码
最近我被分配到了合并正式补丁代码的工作.聊聊整个流程 第一步解压补丁
- Character Studio
- Unity3D 之UGUI 切换开关(toggle)
(toggle)开关能够实现通过点击实现开和关. 开关下面有两个游戏元素,一个是背景,一个是文字,背景是开关按钮的背景. 可以通过 Is ON开控制开关的开与关. Group -->分组,能够实 ...
- SQL Server调优系列进阶篇 - 查询优化器的运行方式
前言 前面我们的几篇文章介绍了一系列关于运算符的基础介绍,以及各个运算符的优化方式和技巧.其中涵盖:查看执行计划的方式.几种数据集常用的连接方式.联合运算符方式.并行运算符等一系列的我们常见的运算符. ...
- cocoaPods的安装和使用之详细介绍
一,在Mac OS X上安装Ruby运行环境 步骤1------安装RVM $ curl -L https://get.rvm.io | bash -s stable 然后载入RVM环境 $ sour ...
- iOS开发——文本高度
1.简单的计算文本高度 // 要计算的文本内容 NSString *testString = @"刘成利,软件工程专业毕业,iOS开发者,目前工作于北京,在证券金融领域从事iOS App开发 ...
- redis基本数据类型【3】-List类型
列表类型采用双向链表实现的,借助列表类型redis还可以作为队列使用,一个列表类型key最多能容纳2^32-1个元素 1.向列表两端增加元素 #向左边增加元素,返回增加后列表的长度 lpush key ...
- ASP.NET中的母版页
添加一个"母版页",使用<asp:ContentPlaceHolder>挖坑,新建的母版页已经自动设置了两个ContentPlaceHolder创建使用母版页的具体页面 ...
- [Leveldb源码剖析疑问]-block_builder.cc之Add函数
Add函数是给一个Data block中添加对应的key和value,函数源码如下,其中有一处不理解: L30~L34是更新last_key_的,不理解这里干嘛不直接last_key_ = key.T ...