Ladies' Choice

Time Limit: 6000ms
Memory Limit: 131072KB

This problem will be judged on UVALive. Original ID: 3989
64-bit integer IO format: %lld      Java class name: Main

Background

Teenagers from the local high school have asked you to help them with the organization of next years 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.

Problem

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

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 the class. Next, there are N lines, each one containing the all the integers from 1 to N, ordered according to the girls preferences. Next, there are N lines, each one containing all the integers from 1 to N, ordered according to the boys preferences.

 

Output

The output for each dataset consists of a sequence of N lines, where the i-th line contains the number of the boy assigned to the i-th girl (from 1 to N). Print a blank line between datasets.

 

Sample 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

Sample Output

1

2

5

3

4

Source

 
解题:稳定婚姻问题
 
男士按照各位女士在自己心目中的地位从高到底,依次求婚。
 
如果女士先前没有对象,被求婚,那么暂且凑一对,
如果女士先前有对象,如果此时求婚的男士比她原来的那个对象在她心目中的地位更高,那么与原来的那个男士解除关系,原来的男士变成光棍。
与新求婚的那位男士建立对象关系。如果此时求婚的男士没有女士原先的男士在她心中地位高,则此时的男士继续光棍着。
 
直到没有光棍为止,此时的婚姻关系稳定。
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int n,mr[maxn][maxn],miss[maxn][maxn];
int wife[maxn],husband[maxn],nxt[maxn];
queue<int>q;
void engage(int man,int woman) {
if(husband[woman]) {
wife[husband[woman]] = ;
q.push(husband[woman]);
}
husband[woman] = man;
wife[man] = woman;
}
void GaleShapley() {
while(!q.empty()) {
int man = q.front();
q.pop();
int woman = mr[man][nxt[man]++];
if(!husband[woman]) engage(man,woman);
else if(miss[woman][husband[woman]] > miss[woman][man])
engage(man,woman);
else q.push(man);
}
}
int main() {
int kase,tmp;
scanf("%d",&kase);
while(kase--) {
scanf("%d",&n);
while(!q.empty()) q.pop();
for(int i = ; i <= n; ++i) {
for(int j = ; j <= n; ++j)
scanf("%d",mr[i]+j);
nxt[i] = ;
wife[i] = ;
q.push(i);
}
for(int i = ; i <= n; ++i) {
for(int j = ; j <= n; ++j) {
scanf("%d",&tmp);
miss[i][tmp] = j;
}
husband[i] = ;
}
GaleShapley();
for(int i = ; i <= n; ++i)
printf("%d\n",wife[i]);
if(kase) putchar('\n');
}
return ;
}

UVALive 3989 Ladies' Choice的更多相关文章

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

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

  2. UVALive 3989 Ladies&#39; Choice

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

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

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

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

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

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

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

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

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

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

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

  8. UVA 1175 - Ladies' Choice

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

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

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

随机推荐

  1. 【XSY2892】【GDSOI2018】谁是冠军

    题目来源:noi2018模拟测试赛(二十三)T3 san 为什么noi模拟赛里会做到省选原题啊…… 题意: Description 有n个人,简单起见把他们编号为1到n,每个人有三项指标分别是攻击力, ...

  2. js倒计时demo 天/时/分/秒

    <html><head> <meta charset="UTF-8"> <title>js简单时分秒倒计时</title> ...

  3. Vue学习之路第八篇:事件修饰符

    学习准备: ①.顾名思义,“事件修饰符”那么肯定是用来修饰事件,既然和事件有关系,那么肯定和“v-on”指令(也可简写为:@)有关系了. ②.事件修饰符有以下几类: .stop:阻止冒泡 .preve ...

  4. 四、分布式 Git(未完待续)

    一.分布式工作流程 在 Git 中,每个开发者同时扮演着节点和集线器的角色——也就是说,每个开发者既可以将自己的代码贡献到其他的仓库中,同时也能维护自己的公开仓库,让其他人可以在其基础上工作并贡献代码 ...

  5. Memcached的实战笔记

    官网:http://memcached.org/ 优秀Blogs: http://blog.csdn.net/jingqiang521/article/details/48345021 开启telne ...

  6. Django REST Framework - 分页 - 渲染器 - 解析器

    为什么要使用分页? 我们数据表中可能会有成千上万条数据,当我们访问某张表的所有数据时,我们不太可能需要一次把所有的数据都展示出来,因为数据量很大,对服务端的内存压力比较大还有就是网络传输过程中耗时也会 ...

  7. DQL命令(查询)

     select *或字段1,字段2...     from 表名     [where 条件]       提示:*符号表示取表中所有列:没有where语句表示        查询表中所有记录:有wh ...

  8. WinServer-IIS-js无法加载问题

    IIS中无法加载JS文件错误 尝试下面的几种解决方法,一起用

  9. C#-委托 lambda 匿名方法 匿名类型

    1.lambda 匿名方法 匿名类型 delegate void d1(); d1 d = delegate(){Console.WriteLine("this is a test" ...

  10. pip安装-mac电脑

    If you meant "pip" specifically: Homebrew provides pip via: `brew install python`. However ...