题目链接:

题目

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不是舞伴;
  • 他们喜欢对方的程度都大于各自当前舞伴的程度。

现在要求对每一个女生,在所有可能和她跳舞的男生中,找出她最喜欢的那个。

题解

裸的稳定婚姻问题。

代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. using namespace std;
  6. const int maxn = 1111;
  7. int pre[maxn][maxn], order[maxn][maxn], ne[maxn];
  8. int future_husband[maxn], future_wife[maxn];
  9. queue<int> Q;
  10. int n;
  11. void engage(int man, int woman) {
  12. int m = future_husband[woman];
  13. if (m) {
  14. future_wife[m] = 0;
  15. Q.push(m);
  16. }
  17. future_wife[man] = woman;
  18. future_husband[woman] = man;
  19. }
  20. void init() {
  21. while (!Q.empty()) Q.pop();
  22. memset(future_husband, 0, sizeof(future_husband));
  23. memset(future_wife, 0, sizeof(future_wife));
  24. memset(ne, 0, sizeof(ne));
  25. }
  26. int main() {
  27. int tc;
  28. scanf("%d", &tc);
  29. while (tc--) {
  30. scanf("%d", &n);
  31. init();
  32. for (int i = 1; i <= n; i++) {
  33. for (int j = 1; j <= n; j++) {
  34. scanf("%d", &pre[i][j]);
  35. }
  36. Q.push(i);
  37. }
  38. for (int i = 1; i <= n; i++) {
  39. for (int j = 1; j <= n; j++) {
  40. int x; scanf("%d", &x);
  41. order[i][x] = j;
  42. }
  43. }
  44. while (!Q.empty()) {
  45. int man = Q.front(); Q.pop();
  46. int woman = pre[man][++ne[man]];
  47. if (!future_husband[woman] || order[woman][man]<order[woman][future_husband[woman]]) {
  48. engage(man, woman);
  49. }
  50. else Q.push(man);
  51. }
  52. for (int i = 1; i <= n; i++) printf("%d\n", future_wife[i]);
  53. if (tc) printf("\n");
  54. }
  55. return 0;
  56. }

UVA 1175 Ladies' Choice 稳定婚姻问题的更多相关文章

  1. UVA 1175 - Ladies' Choice

    1175 - Ladies' Choice 链接 稳定婚姻问题. 代码: #include<bits/stdc++.h> using namespace std; typedef long ...

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

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

  3. UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)

    题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. CSS之全屏背景图

    吐槽啦:Yeah  明天就是国庆了o(* ̄▽ ̄*)o!哈哈,提前祝福各位园友国庆快乐.假期愉快.生活美满.天天开心!国庆我要回家一趟,把一些不用的东西带回家,走访一下亲朋好友,在家打几天酱油~~~ 言 ...

  2. js中关于原型的几个方法

    一.isPrototypeOf()方法,判断一个对象是否是另一个对象的原型 function Student(name,age){ this.name=name; this.age=age; } va ...

  3. Swift字典

    字典初始化 基本语法: [key 1: value 1, key 2: value 2, key 3: value 3] var   airports:    Dictionary<String ...

  4. Lambda表达式的本质是匿名函数

    1.委托的简介: 委托可以简单的理解为方法的列表,添加的方法的参数类型,个数,顺序必须和委托一致, 也就是说委托起到了托管方法的作用,并且约束了要调用的方法. //1声明委托 public deleg ...

  5. 20150214—winform中使用构造函数传值

    构造函数,在函数初始化时就会执行的函数方法,在创建一个类之后,系统会自动在此类中生成一个与类名相同的函数,其中只包含一句代码: InitializeComponent(); 新建一个名字相同的函数,然 ...

  6. AMQ学习笔记 - 13. Spring-jms的配置

    概述 如何使用spring-jms来简化jms客户端的开发? 这篇文章主要记录如何配置以便以后复用,而非原理的讲解,有些内容我 没有掌握原理. producer端 producer端负责发送,这里使用 ...

  7. 浅谈JavaScript中的apply,call和bind

    apply,call,bine 这三兄弟经常让初学者感到疑惑.前两天准备面试时特地做了个比较,其实理解起来也不会太难. apply MDN上的定义: The apply() method calls ...

  8. iOS Foundation框架 -3.利用NSNumber和NSValue将非OC对象类型数据存放到集合

    1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...

  9. “==”,比较的是引用 “equals方法”比较的是具体内容

    package com.java1234.chap03.sec08; public class Demo2 { public static void main(String[] args) { //“ ...

  10. Perl的主要应用领域

    1.Unix系统的维护功能    如我们在前面所说的,Perl可以作为传统Unix系统维护工具的替代,在这方面,它可以对文本文件,特别是对配置文件(还记不记得在配置Linux系统中的文本方式的配置)进 ...