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. These relative modules were not found:...{"sourceM ap":false} 报错解决

    今天在使用vue2.0 + webpack 时,没有动过任何配置文件,也没更新依赖,但是报下面的错误: These relative modules were not found: * ./star1 ...

  2. shell试题

    1.按单词出现频率降序排序! 2.按字母出现频率降序排序! The months of learning in Old Boy education are the few months that I ...

  3. HDU3001 Traveling (状压dp+三进制+Tsp问题总结)

    (1)这道题最多可以走两次,所以有0, 1, 2三种状态,所以我们要用三进制 如果要用三进制,就要自己初始化两个数组, 一个是3的n次方,一个是三进制数的第几位的数字是什么 void init() { ...

  4. shell如何更改当前工作路径

    转载: http://imysqldba.blog.51cto.com/1222376/616805 shell 脚本执行有三种方法 bash 脚本名 sh 脚本名 chmod +x 脚本名 使用下面 ...

  5. iipccsxxtnsoiq

    gxspvyheuetwqgnbwmwd

  6. Java类载入器

    1.   系统载入器简单介绍 Java虚拟机中能够安装多个类载入器,系统默认三个主要类载入器(BootStrap.ExtClassLoader.AppClassLoader).每一个类载入器负责载入特 ...

  7. 如何让Java写的程序,脱离Eclipse在别人的电脑上运行?

    Java程序运行,离不开JRE. 将JRE文件夹拷贝出来, 将编写程序的bin文件夹拷贝出来, 再编写一个bat文件. start jre\bin\javaw.exe -cp .\bin\ -Djav ...

  8. nyoj--214--单调递增子序列(二)(二分查找+LIS)

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序 ...

  9. 14.boost最小生成树 kruskal_min_spainning_tree

    #include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...

  10. MetaSploit攻击实例讲解------社会工程学set攻击(kali linux 2016.2(rolling))(详细)

    不多说,直接上干货! 首先,如果你是用的BT5,则set的配置文件是在 /pentest/exploits/set/set_config下. APACHE_SERVER=ONSELF_SIGNED_A ...