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. HDU 6149 Valley Numer II (状压DP 易错题)

    题目大意:给你一个无向连通图(n<=30),点分为高点和低点,高点数量<=15,如果两个高点和低点都直接连边,那么我们称这三个点形成一个valley,每个点最多作为一个valley的组成部 ...

  2. [读书笔记] R语言实战 (五) 高级数据管理

    1. 数值函数 1) 数学函数 2) 统计函数 3. 数据标准化 scale() 函数对矩阵或者数据框的指定列进行均值为0,标准化为1的标准化 mydata <- data.frame(c1=c ...

  3. 2019-03-19 SQL Server简单存储过程的创建 删除 执行

    --创建名为 Get 的有输入参数的存储过程 create proc Get --设置默认值 @TrustId int ='001' as begin select * from [DealStruc ...

  4. Nutch命令大全

    Nutch采用了一种命令的方式进行工作,其命令可以是对局域网方式的单一命令也可以是对整个Web进行爬取的分步命令.主要的命令如下: 1. Crawl Crawl是"org.apache.nu ...

  5. ASP.NET-使用json

    数据格式 vat strJson =' {"name":"jingya","Age":88} '; // 数字不用写双引号 JSON.par ...

  6. webstorm卡顿问题处理

    webstorm卡顿问题处理 学习了:http://blog.csdn.net/qq673318522/article/details/50583831 找到WebStorm.exe.vmoption ...

  7. C++ OTL MySQL(Windows/Linux) V8.1

    Windows每秒钟10000条以上插入:Linux每秒插入300条以上.Q269752451 输出截图: Linux输出: Windows输出: 有须要的联系 QQ 3508551694 water ...

  8. Codeforce 163 A. Substring and Subsequence DP

    A. Substring and Subsequence   One day Polycarpus got hold of two non-empty strings s and t, consist ...

  9. kentico中的urls

    alias是默认的访问页面 page aliases中可以手动指定访问一个url,然后跳转到当前的页面

  10. 火狐访问IIS出现404,而Chrome可以正常访问

    需要在web.config中的handlers中添加如下节点,保存之后,需要重启电脑. <remove name="ExtensionlessUrlHandler-Integrated ...