UVALive 3989Ladies' Choice(稳定婚姻问题)
题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号
分析:如果是求女生的最好情况下,就要从女生开始选,这样女生都是从最好的到不好的来选,而男生却相反--只能娶那些自己有可能最没好感的女生,因为男生是被动的,他最喜欢的女生不见的会向他求婚。
刘汝佳书上命名错了,so也跟着把男生当成女生了,懒得改命名了,
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int Max = ;
int pref[Max][Max], order[Max][Max], Next[Max];
int future_hasband[Max], future_wife[Max];
queue <int> q; //单身女生队列
void engage(int man, int woman)
{
int m = future_hasband[woman];
if(m)
{
future_wife[m] = ;
q.push(m);
}
future_hasband[woman] = man;
future_wife[man] = woman;
return;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
while(!q.empty())
q.pop();
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
scanf("%d", &pref[i][j]);
future_wife[i] = ; // 女生i没有匹配
Next[i] = ; // 都是从第一个开始查找
q.push(i); // 加入单身队列
}
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
int x;
scanf("%d", &x);
order[i][x] = j; // 表示男生i 对 女生 x 的好感等级
}
future_hasband[i] = ;
}
while(!q.empty())
{
int man = q.front();
q.pop();
int woman = pref[man][Next[man]++];
if(future_hasband[woman] == ) // 表示这个男生并没有 匹配
engage(man, woman);
else if(order[woman][man] < order[woman][future_hasband[woman]]) // 虽然已经匹配了,但是同已经匹配的好感层度来看,更喜欢这个
engage(man, woman);
else
q.push(man); // 否则继续单身,等待下次解救
}
for(int i = ; i <= n; i++)
printf("%d\n", future_wife[i]);
if(T)
printf("\n");
} return ;
}
UVALive 3989Ladies' Choice(稳定婚姻问题)的更多相关文章
- 训练指南 UVALive - 3989(稳定婚姻问题)
ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- LA 3989 - Ladies' Choice 稳定婚姻问题
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法
题目链接:https://vjudge.net/problem/UVALive-3989 题解: 题意:有n个男生和n个女生.每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行. ...
- Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- UVALive-3989 Ladies' Choice (稳定婚姻问题)
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
- HDU1522 稳定婚姻匹配 模板
Marriage is Stable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...
随机推荐
- Reflection和Expression Tree解析泛型集合快速定制特殊格式的Json
很多项目都会用到Json,而且大部分的Json都是格式固定,功能强大,转换简单等,标准的key,value集合字符串:直接JsonConvert.SerializeObject(List<T&g ...
- 中间件(middlebox)
Middleboxes (also known as network functions) are systems that perform sophisticated and often state ...
- js简易函数性能测试器
如果你不想用浏览器的js性能测试工具,可以用下面这个简单的函数测试一下(1毫秒一下的就测不出来了) function testFn(fn,param){ var start = new Date(). ...
- python内置数据类型-字典和列表的排序 python BIT sort——dict and list
python中字典按键或键值排序(我转!) 一.字典排序 在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序. Py ...
- <string> 与<string.h>、<cstring>的区别
<string.h> <string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <cstring> 在C++标准化(1998年 ...
- [转]NullPointerException异常
原文地址:http://blog.csdn.net/javaeeteacher/article/details/4285488 顾名思义,NullPointerException是空指针异常.但是在J ...
- 关于php插件pdo_mysql的安装
今天在做一个商城的连接的时候,需要MpDO验证.需要安装pdo_mysql模块,刚开始按照php扩展模块的安装按照这个安装ZIP,curl都成功了但是安装pdo_mysql却不行,在./configu ...
- GisUtil工具类:将WKT(wellKnownText)文本转换为ElasticSearch识别的空间对象字符串形式
ElasticSearch中的Geo Shape Type: https://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-g ...
- NHibernate中session.update()及session.merge()的区别
今天的工作中遇到一个奇怪的问题,如下: "a different object with the same identifier value was already associated w ...
- VS 远程发布IIS
<?xml version="1.0" encoding="utf-8"?><!--您 Web 项目的发布/打包进程将使用此文件.您可以通过编 ...