博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVALive 3989 Ladies' Choice
阅读量:6825 次
发布时间:2019-06-26

本文共 3195 字,大约阅读时间需要 10 分钟。

经典的稳定婚姻匹配问题

UVALive - 3989
Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   []   []  

Description

Background

Teenagers from the local high school have asked you to help them with the organization of next yearÕs 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 integerN, 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 girlÕs preferences. Next, there are N lines, each one containing all the integers from 1 to N, ordered according to the boyÕs 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

Southwestern 2007-2008

[]   []   []  

#include 
#include
#include
#include
#include
using namespace std;const int maxn=1100;int n;int perfect_boy[maxn][maxn];int perfect_girl[maxn][maxn];int future_husband[maxn],future_wife[maxn];int next[maxn];queue
q;void engage(int boy,int girl){ int m=future_husband[girl]; if(m) { future_wife[m]=0; q.push(m); } future_husband[girl]=boy; future_wife[boy]=girl;}bool lover(int m1,int m2,int girl){ for(int i=1;i<=n;i++) { if(perfect_boy[girl][i]==m1) return true; if(perfect_boy[girl][i]==m2) return false; }}int main(){ int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d",&n); memset(perfect_boy,0,sizeof(perfect_boy)); memset(perfect_girl,0,sizeof(perfect_girl)); memset(future_husband,0,sizeof(future_husband)); memset(future_wife,0,sizeof(future_wife)); memset(next,0,sizeof(next)); while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&perfect_girl[i][j]); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) scanf("%d",&perfect_boy[i][j]); q.push(i); } while(!q.empty()) { int boy=q.front(); q.pop(); int girl=perfect_girl[boy][++next[boy]]; if(future_husband[girl]==0) engage(boy,girl); else { int m=future_husband[girl]; if(lover(boy,m,girl)) engage(boy,girl); else q.push(boy); } } for(int i=1;i<=n;i++) printf("%d\n",future_wife[i]); if(T_T) putchar(10); } return 0;}

转载地址:http://kgezl.baihongyu.com/

你可能感兴趣的文章
Snapcraft 2.38发布:对Classic Snap更加友好
查看>>
我的友情链接
查看>>
MDT 2013 Update 1 Pre部署 Windows 10之MDT 2013安装配置
查看>>
远程调试 Azure Web App
查看>>
活字格企业Web应用生成器V3.0发布更新,支持插件管理和多人协作开发
查看>>
Webpack4教程 - 第三部分,如何使用插件
查看>>
Linux系统的命令提示符及命令格式说明
查看>>
vmware esxi6.0安装
查看>>
我的友情链接
查看>>
java.util.concurrent系列之--CountDownLatch
查看>>
虚拟机中linux网络不通问题解决方式记录
查看>>
游戏开发SpriteKit基础
查看>>
制作自己的多媒体个性相册(下篇)
查看>>
(28)SpringBoot启动时的Banner设置【从零开始学Spring Boot】
查看>>
Exception in thread "ContainerBackgroundProcessor[StandardEngine[Catalina]]"
查看>>
10月30日云栖精选夜读:云栖大会Serverless场分享:日志处理挑战与展望
查看>>
【基础算法】堆排序
查看>>
老男人面试第十家-跨境电商~第五家b轮第六家中兴复试
查看>>
分享实录|以太坊开发需知
查看>>
fabric-sdk-java 1.4安装说明
查看>>