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 ...
随机推荐
- Xcode10适配——Error:Multiple commands produce
今天苹果正式推送了iOS12,今天上午就更新了最新的iOS,及Xcode10.这次更新还行,不需要我们对以前的项目紧急修复,大动手术. 用Xcode10跑之前的项目,也就报了一种类型的错误:Multi ...
- Jquery JS 全局变量
window["a1"]="abc";window["b1"]=5;
- Vue的数据依赖实现原理简析
首先让我们从最简单的一个实例Vue入手: const app = new Vue({ // options 传入一个选项obj.这个obj即对于这个vue实例的初始化 }) 通过查阅文档,我们可以知道 ...
- Mysql学习总结(30)——MySQL 索引详解大全
什么是索引? 1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引,会保存在额外的文件中. 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类 ...
- 高级函数-case
case函数 (适合区间,>,<判断) case when 判断表达式 then when 判断表达式 then ..... end s ...
- HDU 1350 Taxi Cab Scheme
Taxi Cab Scheme Time Limit: 10000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...
- ActiveMQ_Windows和Linux版本的安装部署
1, 保证电脑上安装了jdk6以上版本的java,并配置了好环境变量 : 2, 官方下载地址:http://activemq.apache.org/download-archives.html ,这里 ...
- java之 ------ DAO设计模式的【具体解释】及常见设计模式的【应用】
DAO Data Access Object(数据訪问接口) 一.场景和问题 在Java程序中.常常须要把数据持久化,也须要获取持久化的数据.可是在进行数据持久化的过程中面临诸多问题(如:数据源 不同 ...
- Android,iOS打开手机QQ与指定用户聊天界面
在浏览器中能够通过JS代码打开QQ并弹出聊天界面.一般作为客服QQ使用. 而在移动端腾讯貌似没有发布提供相似API,可是却能够使用schema模式来启动手机QQ. 下面为详细代码: Android: ...
- 对象不支持“abigimage”属性或方法
在一个网页中用了一个js插件, js文件引用的没有错,代码也和demo差点儿相同, 可是执行时ie的调试工具报了一个错: 解决方式: jquery文件冲突,发现原来自己引过一个 <script ...