题目大意:稳定婚姻问题。。。。

题目分析:模板题。

代码如下:

  1. # include<iostream>
  2. # include<cstdio>
  3. # include<queue>
  4. # include<cstring>
  5. # include<algorithm>
  6. using namespace std;
  7. # define LL long long
  8. # define REP(i,s,n) for(int i=s;i<n;++i)
  9. # define CL(a,b) memset(a,b,sizeof(a))
  10. # define CLL(a,b,n) fill(a,a+n,b)
  11.  
  12. const int N=1005;
  13. int fu_husband[N],fu_wife[N],order[N][N],pref[N][N],nxt[N],n;
  14. queue<int>q;
  15.  
  16. void engage(int man,int woman)
  17. {
  18. int k=fu_husband[woman];
  19. if(k){
  20. fu_wife[k]=0;
  21. q.push(k);
  22. }
  23. fu_wife[man]=woman;
  24. fu_husband[woman]=man;
  25. }
  26.  
  27. int main()
  28. {
  29. int T;
  30. scanf("%d",&T);
  31. while(T--)
  32. {
  33. scanf("%d",&n);
  34. while(!q.empty()) q.pop();
  35. REP(i,1,n+1){
  36. REP(j,1,n+1) scanf("%d",&pref[i][j]);
  37. nxt[i]=1;
  38. fu_wife[i]=0;
  39. q.push(i);
  40. }
  41. REP(i,1,n+1){
  42. REP(j,1,n+1){
  43. int x;
  44. scanf("%d",&x);
  45. order[i][x]=j;
  46. }
  47. fu_husband[i]=0;
  48. }
  49. while(!q.empty()){
  50. int man=q.front();
  51. q.pop();
  52. int woman=pref[man][nxt[man]++];
  53. if(!fu_husband[woman])
  54. engage(man,woman);
  55. else if(order[woman][man]<order[woman][fu_husband[woman]])
  56. engage(man,woman);
  57. else
  58. q.push(man);
  59. }
  60. REP(i,1,n+1) printf("%d\n",fu_wife[i]);
  61. if(T) printf("\n");
  62. }
  63. return 0;
  64. }

  

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

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

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

  2. UVALive 3989 Ladies' Choice

    Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...

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

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

  4. UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)

    题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...

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

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

  6. UVALive 3989 Ladies&#39; Choice

    经典的稳定婚姻匹配问题 UVALive - 3989 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format:  ...

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

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

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

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

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

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

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

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

随机推荐

  1. 让ASP.NET OutputCache使用http.sys kernel-mode cache

    在默认情况下,http.sys kerne mode cache只缓存静态文件. 那我们如何让ASP.NET OutputCache直接使用http.sys kerne mode cache?这样缓存 ...

  2. Mysql和sqlite数据库操作心得

    经过最近一段时间的实际工作发现,原来只是认为Mysql和sqlite是分别独立的,数据传输和共享或有障碍,其实这是一个误区.当我们想要将sqlite中的数据存放到mysql中,最好的方法就是利用中间文 ...

  3. scrapy爬虫系列之六--模拟登录

    功能点:如何发送携带cookie访问登录后的页面,如何发送post请求登录 爬取网站:bilibili.github 完整代码:https://files.cnblogs.com/files/book ...

  4. Python开发【模块】:M2Crypto RSA加密、解密

    M2Crypto 模块 快速安装: # 环境centos7.0,提前装好openssl(自行百度安装),windows装不上,暂不考虑了 [root@localhost ~]# pip install ...

  5. Python性能鸡汤(转)

    英文原文:http://blog.monitis.com/index.php/2012/02/13/python-performance-tips-part-1/ 英文原文:http://blog.m ...

  6. 1107 Social Clusters[并查集][难]

    1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...

  7. JOJ1202。重新操刀ACM,一天一练!做个简单的题目温习。

    http://ac.jobdu.com/problem.php?pid=1202 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100).   ...

  8. mono安装

    linux上的DotNET,安装mono 当前,在Linux系统上架设ASP.NET网站.建设WEB应用工程项目已经在国内流行起来,而“Mono+Jexus”架构模式是Linux承载ASP.NET企业 ...

  9. python开发之路目录

    Python 目录 基础 python入门 python数据类型.字符编码.文件处理 python函数基础 python函数进阶 python装饰器函数 python装饰器函数 python递归函数 ...

  10. uva11732 Trie转化

    有40001 个单词每个单词长度不超过1000,每个两个单词之间都要比较求要比较次数 int strcmp(char *s,char *t){ int i; for(i = 0; s[i]==t[i] ...