UVALive 3989 Ladies' Choice
Ladies' Choice
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的更多相关文章
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
- UVALive 3989 Ladies' Choice
经典的稳定婚姻匹配问题 UVALive - 3989 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: ...
- LA 3989 - Ladies' Choice 稳定婚姻问题
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 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(稳定婚姻问题)
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 问题 ...
- UVA 1175 - Ladies' Choice
1175 - Ladies' Choice 链接 稳定婚姻问题. 代码: #include<bits/stdc++.h> using namespace std; typedef long ...
- UVALive-3989 Ladies' Choice (稳定婚姻问题)
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
随机推荐
- 【XSY2892】【GDSOI2018】谁是冠军
题目来源:noi2018模拟测试赛(二十三)T3 san 为什么noi模拟赛里会做到省选原题啊…… 题意: Description 有n个人,简单起见把他们编号为1到n,每个人有三项指标分别是攻击力, ...
- js倒计时demo 天/时/分/秒
<html><head> <meta charset="UTF-8"> <title>js简单时分秒倒计时</title> ...
- Vue学习之路第八篇:事件修饰符
学习准备: ①.顾名思义,“事件修饰符”那么肯定是用来修饰事件,既然和事件有关系,那么肯定和“v-on”指令(也可简写为:@)有关系了. ②.事件修饰符有以下几类: .stop:阻止冒泡 .preve ...
- 四、分布式 Git(未完待续)
一.分布式工作流程 在 Git 中,每个开发者同时扮演着节点和集线器的角色——也就是说,每个开发者既可以将自己的代码贡献到其他的仓库中,同时也能维护自己的公开仓库,让其他人可以在其基础上工作并贡献代码 ...
- Memcached的实战笔记
官网:http://memcached.org/ 优秀Blogs: http://blog.csdn.net/jingqiang521/article/details/48345021 开启telne ...
- Django REST Framework - 分页 - 渲染器 - 解析器
为什么要使用分页? 我们数据表中可能会有成千上万条数据,当我们访问某张表的所有数据时,我们不太可能需要一次把所有的数据都展示出来,因为数据量很大,对服务端的内存压力比较大还有就是网络传输过程中耗时也会 ...
- DQL命令(查询)
select *或字段1,字段2... from 表名 [where 条件] 提示:*符号表示取表中所有列:没有where语句表示 查询表中所有记录:有wh ...
- WinServer-IIS-js无法加载问题
IIS中无法加载JS文件错误 尝试下面的几种解决方法,一起用
- C#-委托 lambda 匿名方法 匿名类型
1.lambda 匿名方法 匿名类型 delegate void d1(); d1 d = delegate(){Console.WriteLine("this is a test" ...
- pip安装-mac电脑
If you meant "pip" specifically: Homebrew provides pip via: `brew install python`. However ...