The Stable Marriage Problem
 

Description

The stable marriage problem consists of matching members of two different sets according to the member’s preferences for the other set’s members. The input for our problem consists of:

  • a set M of n males;
  • a set F of n females;
  • for each male and female we have a list of all the members of the opposite gender in order of preference (from the most preferable to the least).

A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (mf) such that f ∈ F prefers m ∈ M to her current partner and m prefers f over his current partner. The stable marriage A is called male-optimal if there is no other stable marriage B, where any male matches a female he prefers more than the one assigned in A.

Given preferable lists of males and females, you must find the male-optimal stable marriage.

Input

The first line gives you the number of tests. The first line of each test case contains integer n (0 < n < 27). Next line describes n male and n female names. Male name is a lowercase letter, female name is an upper-case letter. Then go n lines, that describe preferable lists for males. Next n lines describe preferable lists for females.

Output

For each test case find and print the pairs of the stable marriage, which is male-optimal. The pairs in each test case must be printed in lexicographical order of their male names as shown in sample output. Output an empty line between test cases.

Sample Input

2
3
a b c A B C
a:BAC
b:BAC
c:ACB
A:acb
B:bac
C:cab
3
a b c A B C
a:ABC
b:ABC
c:BCA
A:bac
B:acb
C:abc

Sample Output

a A
b B
c C a B
b A
c C

Source

 
 
【题意】
  自己看
 
【分析】
  怎么感觉稳定婚姻问题都没有其他问法。。。
  做的两题都是一样的。。。
  这题是male-optimal,就练练模版了。。。
 
代码如下:
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 40 int ml[Maxn][Maxn],rk[Maxn][Maxn];
int fw[Maxn],fh[Maxn],nt[Maxn]; int n;
// bool pm[Maxn],pf[Maxn];
char s[Maxn]; queue<int > q; void init()
{
scanf("%d",&n);
// memset(pm,0,sizeof(pm));
// memset(pf,,sizeof(pf));
for(int i=;i<=n;i++)
{
scanf("%s",s);
// pm[s[0]-'a'+1]=1;
}
for(int i=;i<=n;i++)
{
scanf("%s",s);
// pf[s[0]-'A'+1]=1;
}
while(!q.empty()) q.pop();
memset(fw,,sizeof(fw));
memset(fh,,sizeof(fh));
for(int i=;i<=n;i++)
{
scanf("%s",s);
int x=s[]-'a'+;
for(int j=;j<=n;j++)
{
int y=s[j+]-'A'+;
ml[x][j]=y;
}
fw[x]=;nt[x]=;
q.push(x);
}
for(int i=;i<=n;i++)
{
scanf("%s",s);
int x=s[]-'A'+;
for(int j=;j<=n;j++)
{
int y=s[j+]-'a'+;
rk[x][y]=j;
}
fh[x]=;
}
} void engage(int x,int y)
{
int z=fh[y];
if(z)
{
fw[z]=;
q.push(z);
}
fw[x]=y;fh[y]=x;
} void ffind()
{
while(!q.empty())
{
int x=q.front();q.pop();
int y=ml[x][nt[x]++];
if(fh[y]==||rk[y][x]<rk[y][fh[y]])
engage(x,y);
else q.push(x);
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
ffind();
for(int i=;i<=;i++) if(fw[i])
{
printf("%c %c\n",'a'+i-,'A'+fw[i]-);
}
if(T) printf("\n");
}
return ;
}

[POJ3487]

这样我还WA了两次,next不记得++,还有最好清零用memset全清了来得干净~~

2016-10-26 20:51:44

【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)的更多相关文章

  1. poj 3478 The Stable Marriage Problem 稳定婚姻问题

    题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们全部人的婚姻都是稳定的. 所谓不稳婚姻是说.比方说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的 ...

  2. POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)

    Description The stable marriage problem consists of matching members of two different sets according ...

  3. [POJ 3487]The Stable Marriage Problem

    Description The stable marriage problem consists of matching members of two different sets according ...

  4. 【转】稳定婚姻问题(Stable Marriage Problem)

    转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...

  5. The Stable Marriage Problem

    经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...

  6. HDOJ 1914 The Stable Marriage Problem

    rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 6553 ...

  7. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

  8. 【HDOJ】1914 The Stable Marriage Problem

    稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...

  9. hdoj1435 Stable Match(稳定婚姻问题)

    简单稳定婚姻问题. 题目描述不够全面,当距离相同时容量大的优先选择. 稳定婚姻问题不存在无解情况. #include<iostream> #include<cstring> # ...

随机推荐

  1. linux-ssh远程后台执行脚本-放置后台执行问题(转)

    写了一个监控负载的小脚本(死循环,测试结束后再kill对应进程),因需要监控多台服务器,所以在一台服务器上使用ssh统一执行脚本遇到问题:使用ssh root@172.16.146.20 '/usr/ ...

  2. Ubuntu GNOME 安装日语输入法(收集)

    原网址是:code.duffy.jp/add-japanese-input-to-gnome/ 1)在终端(Ctrl + Alt + t)输入:sudo apt-get install ibus-an ...

  3. C# 右键菜单 contextMenuStrip

    1.添加contextMenuStrip控件 默认命名:contextMenuStrip1 2.在要显示的控件上,找到其ContextMenuStrip属性,并设置其为contextMenuStrip ...

  4. Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面

    Xamarin.iOS - 利用Settings插件与EAIntroView制作App的欢迎界面 关于欢迎界面 很多App第一次启动都会有一个欢迎界面,欢迎界面往往决定这用户对App的第一映像,所以欢 ...

  5. 关于web项目中中文乱码问题的总结

    关于post和get的中文乱码处理 get: (1)转码:String username=request.getParameter("username");       Strin ...

  6. 享元模式(咖啡屋)【java与模式】

    package com.javapatterns.flyweight.coffeeshop; public class Flavor extends Order { private String fl ...

  7. 数学符号π (Pi)、Σ(Capital Sigma)、μ (Mu) 、σ(sigma)、∏(capital pi), ∫(Integral Symbol)的来历

    1.π (Pi; periphery/周长) March 14 marks Pi Day, the holiday commemorating the mathematical constant π ...

  8. Codevs 3731 寻找道路 2014年 NOIP全国联赛提高组

    3731 寻找道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在有向图G中,每条边的长度均为1,现给定起点和终点,请你在图中找 ...

  9. 2、Charm Bracelet( poj 3624)简单0-1背包

    题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostr ...

  10. ASP.NET中的母版页

    添加一个"母版页",使用<asp:ContentPlaceHolder>挖坑,新建的母版页已经自动设置了两个ContentPlaceHolder创建使用母版页的具体页面 ...