经典的稳定婚姻匹配问题

UVALive - 3989

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

[Submit]   [Go Back]   [Status]

Description

Problem I – LadiesÕ Choice

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

[Submit]   [Go Back]   [Status]

#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&#39; Choice的更多相关文章

  1. UVALive 3989 Ladies' Choice

    Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...

  2. UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)

    题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...

  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. 如何在自己的Activity中去控制EditText的焦点

    在进入一个Activity时,如果这个Activity中有EditText,则这个EditText会自动获取焦点,然后就会弹出软键盘,这样给用户体验不是很好.所以一般会通过代码控制让EditText不 ...

  2. No.3小白的HTML+CSS心得篇

    A--看的东西多了总会出现好多模糊不清的又长的很像的的词语 今天对此进行区别分析下 1. align 与 text-align的区别 align 在W3Cschool中是这样解释的  ----alig ...

  3. ASP.NET 母版页和内容页的加载顺序

    Master 模板页Content 内容页如果希望Master页面的数据传给Content页面,请Init如果希望Content页面的数据传给Master页面,请重载Load具体细节不多说了,看下面页 ...

  4. Orchard站点性能优化-预热

    Orchard CMS 包含一个 Warmup Module 模块,当我们的站点在共享主机上的时候,它可以显著的帮助我们快速响应用户访问请求.当你开启这个模块以后,你设置的URL的里面的内容会缓存起来 ...

  5. 用ToggleButton和ImageView实现不同状态下显示的切换

    靠,写的时候第一次因为把implements OnCheckedChangeListener这里实现的接口写错了,搞了很久, 后来发现又少了这两句错了 btn = (ToggleButton) fin ...

  6. mac下面xcode+ndk7配置cocos2dx & box2d的跨ios和android平台的游戏教程

    这篇教程是介绍如何使用cocos2d-x和box2d来制作一个demo,且此demo能同时运行于ios和android平台.在继续阅读之前,建议您先阅读上一篇教程. 首先,按照上一篇教程,搭建好mac ...

  7. 菜鸟初识UML

    首当其冲的就是:什么是UML呢? 首先,UML 是一种可视化的面向对象的建模语言.它是一个支持模型化和软件系统开发的图形化语言,为软件开发的所有阶段提供模型化和可视化支持,包括由需求分析到规格,到构造 ...

  8. Linux 环境下 fork 函数和 exec 函数族的使用

    前言 接触 Linux 已经有几个月了,以前在网上看各路大神均表示 Windows 是最烂的开发平台,我总是不以为然,但是经过这段时间琢磨,确实觉得 Linux 开发给我带来不少的便利.下面总结一下学 ...

  9. 网站压力测试之ApacheBench

    ApacheBench是 Apache 附带的一个小工具,专门用于 HTTP Server 的benchmark testing,可以同时模拟多个并发请求.使用yum安装apache,ab工具在/us ...

  10. 51cto培训课程

    课程: 云计算: OpenStack | 虚拟化 | 云平台 | Office 365 | 云服务 | Docker | 其他 大数据: Spark | Hadoop | Storm | Hive | ...