UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接:
题目
Ladies' Choice
Time Limit: 6000MS
Memory Limit: Unknown
64bit IO Format: %lld & %llu
问题描述
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 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.
输出
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.
样例
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
output
1
2
5
3
4
题意
男生女生一一配对且男生u和女生v不存在以下情况:
- 男生u和女生v不是舞伴;
- 他们喜欢对方的程度都大于各自当前舞伴的程度。
现在要求对每一个女生,在所有可能和她跳舞的男生中,找出她最喜欢的那个。
题解
裸的稳定婚姻问题。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1111;
int pre[maxn][maxn], order[maxn][maxn], ne[maxn];
int future_husband[maxn], future_wife[maxn];
queue<int> Q;
int n;
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;
}
void init() {
while (!Q.empty()) Q.pop();
memset(future_husband, 0, sizeof(future_husband));
memset(future_wife, 0, sizeof(future_wife));
memset(ne, 0, sizeof(ne));
}
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
init();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &pre[i][j]);
}
Q.push(i);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x; scanf("%d", &x);
order[i][x] = j;
}
}
while (!Q.empty()) {
int man = Q.front(); Q.pop();
int woman = pre[man][++ne[man]];
if (!future_husband[woman] || order[woman][man]<order[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 (tc) printf("\n");
}
return 0;
}
UVA 1175 Ladies' Choice 稳定婚姻问题的更多相关文章
- UVA 1175 - Ladies' Choice
1175 - Ladies' Choice 链接 稳定婚姻问题. 代码: #include<bits/stdc++.h> using namespace std; typedef long ...
- LA 3989 - Ladies' Choice 稳定婚姻问题
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...
- UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法
题目链接:https://vjudge.net/problem/UVALive-3989 题解: 题意:有n个男生和n个女生.每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行. ...
- UVALive 3989Ladies' Choice(稳定婚姻问题)
题目链接 题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号 分析:如果是求女生的最好情况下, ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...
- UVALive-3989 Ladies' Choice (稳定婚姻问题)
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
- 训练指南 UVALive - 3989(稳定婚姻问题)
ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...
随机推荐
- 坑到了,EF执行带事物的存储过程
用EF开发项目,今天调用 带事物 存储过程,始终报错,"EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配.上一计数 = 1,当前计数 = 0.\r\nEXEC ...
- js 数组的length(javascript教程四)
这是一个简单的函数,就是利用length来判断数组再遍历数组了. <script language="javascript" type="text/javascri ...
- 20150313--AJAX
from,提交信息,抛弃当前页面,并刷新页面. AJAX,从当前页面获取信息,并提交.反馈信息,不会抛弃页面. 例: 用户注册时,验证用户名是否已存在, 创建两个标准控件:TextBox1,Label ...
- Spring IOC整理
示例展示 Spring的一大特点是利用配置的xml文件实现依赖注入. 所谓依赖注入是指把一个业务对象注入另一个业务对象,从而达到对象间的松耦合.(注意是业务对象哦!)依赖注入讲的通俗一点,就是让一个对 ...
- 修改eclipse中tomcat的发布路径
当我们在eclipse部署好tomcat的时候,默认这个项目是部署在eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\t ...
- iOS获取汉字的拼音
在iOS开发中经常涉及到汉字的排序,最常见的就是需要根据首字母的字符顺序排列,比如常见的通讯录等.总结出来,大致可以分为两种方法,其中参考文献[1]中提供的方法十分复杂,利用查表的方法是先,并且代码量 ...
- ubuntu 14.04 下FTP服务器的搭建--锁定用户目录,解决vsftpd: refusing to run with writable root inside chroot()
FTP服务器的搭建,我要实现的需求是: 不允许匿名访问,因为我的机器不想让谁都能登录上来,随便获取文件, 需要锁定一个目录,因为在家里,我需要给媳妇下载一些电影 韩剧之类的东西,媳妇会来我机器下载,但 ...
- Java应用短信猫
首先确定短信猫正常连接到主机,并安装SIM卡.先用超级终端测试短息猫能不能用.安装minicom:#sudo apt-get install minicom安装完成后,执行#sudo minicom ...
- jQuery: 图片不完全按比例自动缩小
有时我们会有这样的需求:让图片显示在固定大小的区域.如果不考虑 IE6 完全可以使用 css 的 max-width 限制宽度自动按比例缩小显示,但是这样有个问题,就是如果按比例缩小后,图片高度不够, ...
- node-mongo-native1.3.19连接mongo的最优方法
最近需要在node下连接mongo,尝试了很多方法,本文简要总结一下 选择Driver 首先,基本上有4个常见的driver供选择 1.官方的是node-mongo-native 2.基于node-m ...