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个男生,每位女生按照她的偏爱程度将男生排序, ...
随机推荐
- 此utf8 非彼utf8 ——谈http协议里的编码问题
我这里看两个编码: BDPAGETYPE:2BDQID:0xc92b034d0bc985e8BDUSERID:809441751 Cache-Control:private Connection:Ke ...
- 仿造slither.io第一步:先画条蛇
前言 最近 slither.io 貌似特别火,中午的时候,同事们都在玩,包括我自己也是玩的不亦乐乎. 好久好久没折腾过canvas相关的我也是觉得是时候再折腾一番啦,所以就试着仿造一下吧.楼主也没写过 ...
- Linux中sudo配置
Linux下的sudo及其配置文件/etc/sudoers的详细配置. 1.sudo介绍 sudo是linux下常用的允许普通用户使用超级用户权限的工具,允许系统管理员让普通用户执行一些或者全部的ro ...
- 总有一天会NB的! SB一样的坚持会有NB一样的结果的!
第一天: 1.背景图不显示,因为背景图片无法撑开div,所以必须自己设置div的高度哦! 2.div水平居中! A:margin-left:auto;margin-right:auto; B: 父元 ...
- SpringMVC学习--校验
简介 项目中,通常使用较多是前端的校验,比如页面中js校验.对于安全要求较高点建议在服务端进行校验. 服务端校验: 控制层conroller:校验页面请求的参数的合法性.在服务端控制层conrolle ...
- java文件上传和下载
简介 文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...
- 安装python-docx
安装环境: windows 7 64位 python 3.4.2 64位 说明: 用pip install python-docx不行,当装lxml时出现错误,一直卡在那. 安装非官方的版本,如下图, ...
- GridView多表关联
有些时候,需要多表关联查询,使用SqlDataProvider比较灵活 Controller中 use yii\data\SqlDataProvider; $dataProvider = new Sq ...
- 初识React
React 是Facebook开源的一个用于构建用户界面的Javascript库,已经 应用于Facebook及旗下Instagram React专注于MVC架构中的V,即视图 React引入了 虚拟 ...
- js 中 == 和=== 有什么区别?
第一个是相等符:第二个全等符: 其中第一个在比较的时候,会进行类型转换,而第二个则不会, alert('55' == 55);//truealert('55' === 55);//false