经典的稳定婚姻匹配问题

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. Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project

    Qt WebKit and HTML5 geolocation | Qt Project forums | Qt Project Qt WebKit and HTML5 geolocation   I ...

  2. 2.3.9 用NPOI操作EXCEL--通过NPOI获得公式的返回值

    前面我们学习了通过NPOI向Excel中设置公式,那么有些读者可能会问:“NPOI能不能获取公式的返回值呢?”,答案是可以!一.获取模板文件中公式的返回值如在D盘中有一个名为text.xls的Exce ...

  3. ubuntu之安装java浏览器插件

    最近搞什么openstack,在浏览器访问远程虚拟机的时候,需要浏览器有支持java.这个之前真没注意过呢, 通过自己的实践写点东西,方便一下你们搞: 1,首先去http://www.java.com ...

  4. 「OC」 多态

    一.基本概念 多态在代码中的体现,即为某一类事物的多种形态,OC对象具有多态性.必须要有继承,没有继承就没有多态. 在使用多态时,会进行动态检测,以调用真实的对象方法. 多态在代码中的体现即父类指针指 ...

  5. Node.js学习笔记3(快速入门)

           一.开始使用Node.js编程           1.hello world           好了,让我们开始实现第一个 Node.js 程序吧.打开你常用的文本编辑器,在其中输入 ...

  6. Ruby学习: 类的定义和实例变量

    ruby是完全面向对象的,所有的数据都是对象,没有独立在类外的方法,所有的方法都在类中定义的. 一.类的定义语法 类的定义以 class 关键字开头,后面跟类名,以 end标识符结尾. 类中的方法以 ...

  7. nginx 引入外部文件

    http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $ ...

  8. DIOR HOMME_百度百科

    DIOR HOMME_百度百科     DIOR HOMME    编辑    Dior Homme 男装品牌,中文名迪奥·桀傲,由迪奥 (Dior) 在2001年更名更来,品牌来源地法国.迪奥·桀傲 ...

  9. Objective中的协议(Protocol)

    Objective中的协议(Protocol) 作用: 专门用来声明一大堆方法. (不能声明属性,也不能实现方法,只能用来写方法的声明). 只要某个类遵守了这个协议.就相当于拥有这个协议中的所有的方法 ...

  10. lib32gcc1 : Depends: gcc-4.9-base (= 4.9-20140406-0ubuntu1) but 4.9.3-0ubuntu4

    运行:sudo apt-get update 然后重新安装lib32gcc1