题目链接

题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号

分析:如果是求女生的最好情况下,就要从女生开始选,这样女生都是从最好的到不好的来选,而男生却相反--只能娶那些自己有可能最没好感的女生,因为男生是被动的,他最喜欢的女生不见的会向他求婚。

刘汝佳书上命名错了,so也跟着把男生当成女生了,懒得改命名了,

  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <cstdio>
  5. #include <queue>
  6. using namespace std;
  7. const int Max = ;
  8. int pref[Max][Max], order[Max][Max], Next[Max];
  9. int future_hasband[Max], future_wife[Max];
  10. queue <int> q; //单身女生队列
  11. void engage(int man, int woman)
  12. {
  13. int m = future_hasband[woman];
  14. if(m)
  15. {
  16. future_wife[m] = ;
  17. q.push(m);
  18. }
  19. future_hasband[woman] = man;
  20. future_wife[man] = woman;
  21. return;
  22. }
  23. int main()
  24. {
  25. int T;
  26. scanf("%d", &T);
  27. while(T--)
  28. {
  29. int n;
  30. scanf("%d", &n);
  31. while(!q.empty())
  32. q.pop();
  33. for(int i = ; i <= n; i++)
  34. {
  35. for(int j = ; j <= n; j++)
  36. scanf("%d", &pref[i][j]);
  37. future_wife[i] = ; // 女生i没有匹配
  38. Next[i] = ; // 都是从第一个开始查找
  39. q.push(i); // 加入单身队列
  40. }
  41. for(int i = ; i <= n; i++)
  42. {
  43. for(int j = ; j <= n; j++)
  44. {
  45. int x;
  46. scanf("%d", &x);
  47. order[i][x] = j; // 表示男生i 对 女生 x 的好感等级
  48. }
  49. future_hasband[i] = ;
  50. }
  51. while(!q.empty())
  52. {
  53. int man = q.front();
  54. q.pop();
  55. int woman = pref[man][Next[man]++];
  56. if(future_hasband[woman] == ) // 表示这个男生并没有 匹配
  57. engage(man, woman);
  58. else if(order[woman][man] < order[woman][future_hasband[woman]]) // 虽然已经匹配了,但是同已经匹配的好感层度来看,更喜欢这个
  59. engage(man, woman);
  60. else
  61. q.push(man); // 否则继续单身,等待下次解救
  62. }
  63. for(int i = ; i <= n; i++)
  64. printf("%d\n", future_wife[i]);
  65. if(T)
  66. printf("\n");
  67. }
  68.  
  69. return ;
  70. }

UVALive 3989Ladies' Choice(稳定婚姻问题)的更多相关文章

  1. 训练指南 UVALive - 3989(稳定婚姻问题)

    ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...

  2. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  3. LA 3989 - Ladies' Choice 稳定婚姻问题

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  4. UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法

    题目链接:https://vjudge.net/problem/UVALive-3989 题解: 题意:有n个男生和n个女生.每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行. ...

  5. Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法

    /** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...

  6. 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)

    Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...

  7. UVALive-3989 Ladies' Choice (稳定婚姻问题)

    题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...

  8. HDU1522 稳定婚姻匹配 模板

    Marriage is Stable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

随机推荐

  1. 此utf8 非彼utf8 ——谈http协议里的编码问题

    我这里看两个编码: BDPAGETYPE:2BDQID:0xc92b034d0bc985e8BDUSERID:809441751 Cache-Control:private Connection:Ke ...

  2. 仿造slither.io第一步:先画条蛇

    前言 最近 slither.io 貌似特别火,中午的时候,同事们都在玩,包括我自己也是玩的不亦乐乎. 好久好久没折腾过canvas相关的我也是觉得是时候再折腾一番啦,所以就试着仿造一下吧.楼主也没写过 ...

  3. Linux中sudo配置

    Linux下的sudo及其配置文件/etc/sudoers的详细配置. 1.sudo介绍 sudo是linux下常用的允许普通用户使用超级用户权限的工具,允许系统管理员让普通用户执行一些或者全部的ro ...

  4. 总有一天会NB的! SB一样的坚持会有NB一样的结果的!

    第一天: 1.背景图不显示,因为背景图片无法撑开div,所以必须自己设置div的高度哦! 2.div水平居中! A:margin-left:auto;margin-right:auto;  B: 父元 ...

  5. SpringMVC学习--校验

    简介 项目中,通常使用较多是前端的校验,比如页面中js校验.对于安全要求较高点建议在服务端进行校验. 服务端校验: 控制层conroller:校验页面请求的参数的合法性.在服务端控制层conrolle ...

  6. java文件上传和下载

    简介 文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...

  7. 安装python-docx

    安装环境: windows 7 64位 python 3.4.2 64位 说明: 用pip install python-docx不行,当装lxml时出现错误,一直卡在那. 安装非官方的版本,如下图, ...

  8. GridView多表关联

    有些时候,需要多表关联查询,使用SqlDataProvider比较灵活 Controller中 use yii\data\SqlDataProvider; $dataProvider = new Sq ...

  9. 初识React

    React 是Facebook开源的一个用于构建用户界面的Javascript库,已经 应用于Facebook及旗下Instagram React专注于MVC架构中的V,即视图 React引入了 虚拟 ...

  10. js 中 == 和=== 有什么区别?

    第一个是相等符:第二个全等符: 其中第一个在比较的时候,会进行类型转换,而第二个则不会, alert('55' == 55);//truealert('55' === 55);//false