UVALive 3989 Ladies' Choice
经典的稳定婚姻匹配问题
UVALive - 3989
Description BackgroundTeenagers 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, ProblemGiven 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 InputInput 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 OutputThe 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 Input1 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 Output1 2 5 3 4 Source Southwestern 2007-2008
|
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> 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<int> 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;
}
UVALive 3989 Ladies' Choice的更多相关文章
- UVALive 3989 Ladies' Choice
Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
- 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 ...
随机推荐
- HDU 1722 Cake
#include<cstdio> int gcd(int m, int n) { ?n:gcd(n % m, m); } int main() { int m, n; while(scan ...
- js 获取前天、昨天、今天、明天、后天的时间
js 获取前天.昨天.今天.明天.后天的时间 2011-05-19 21:03 <html><head><meta http-equiv="Content- ...
- Python经常使用第三方工具、库、骨架
Python ImagingLibrary(PIL):它提供强大的图形处理的能力,并提供广泛的图形文件格式支持.该库能进行图形格式的转换.打印和显示.还能进行一些图形效果的处理,如图形的放大.缩小和旋 ...
- 初探swift语言的学习—Object-C与Swift混编
swift 语言出来后,可能新的项目直接使用swift来开发,但可能在过程中会遇到一些情况,某些已用OC写好的类或封装好的模块,不想再在swift 中再写一次,哪就使用混编.这个在IOS8中是允许的. ...
- U盘安装Ubuntu14.4时遇到分区问题记录
1.在安装Ubuntu14.4时,遇到如果先分出 / 跟挂载的主分区时,后面只能再分一个swap,或者挂载一个/home,或者一个/ boot 时不能继续分区,当然想安装也是不能不能成功的. 解决办法 ...
- jQuery $.fn.extend方式自定义插件
之前例子是扩展jQuery的工具方法,即通过$.xxx(para);的形式来使用的.下面是扩展jquery对象的方法,即任意一个jquery对象都已访问. 具体如下: wyl.js: (functio ...
- 一、Linux启动过程详解
启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...
- Python之路:Python 函数
一.函数式编程:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装 二. 函数的定义和使用 def 函数名(参数): ... 函数体 ... 函数的定义主要有 ...
- QPushButton跑进度条(使用QSS的不同修饰来实现,其实是伪进度条)
主要用到qlineargradient,写以下CSS样式即可实现: background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, sto ...
- Xamarin.Android开发实践(二)
原文:Xamarin.Android开发实践(二) 一.准备 开始学习本教程前必须先完成该教程http://www.cnblogs.com/yaozhenfa/p/xamarin_android_qu ...