HDOJ 1914 The Stable Marriage Problem

rt 稳定婚姻匹配问题

The Stable Marriage Problem

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 438    Accepted Submission(s): 222

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 (m, f) 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

Southeastern Europe 2007

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

int n;
char boy_name[30][2],girl_name[30][2];
int to_boy[26],to_girl[26];

int perfect_boy[30][30],perfect_girl[30][30];
int future_husband[30],future_wife[30];
int next[30];
queue<int> q;

void init()
{
    memset(boy_name,0,sizeof(boy_name));
    memset(girl_name,0,sizeof(girl_name));
    memset(to_boy,0,sizeof(to_boy));
    memset(to_girl,0,sizeof(to_girl));
    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();
}

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 boy,int m,int girl)
{
    for(int i=1;i<=n;i++)
    {
        if(perfect_boy[girl][i]==boy) return true;
        if(perfect_boy[girl][i]==m) return false;
    }
}

int main()
{
    int T_T,flag=0;
    char in[50];
    scanf("%d",&T_T);
    while(T_T--)
    {
        if(flag==0) flag=1;
        else putchar(10);
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",boy_name[i]);
            to_boy[boy_name[i][0]-'a']=i;
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%s",girl_name[i]);
            to_girl[girl_name[i][0]-'A']=i;
        }
        for(int i=0;i<n;i++)
        {
            scanf("%s",in);
            int boy=to_boy[in[0]-'a'];
            for(int j=2;j<n+2;j++)
            {
                int girl=to_girl[in[j]-'A'];
                perfect_girl[boy][j-1]=girl;
            }
            q.push(i+1);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%s",in);
            int girl=to_girl[in[0]-'A'];
            for(int j=2;j<n+2;j++)
            {
                int boy=to_boy[in[j]-'a'];
                perfect_boy[girl][j-1]=boy;
            }
        }
        while(!q.empty())
        {
            int boy=q.front(); q.pop();
            int girl=perfect_girl[boy][++next[boy]];
            int m=future_husband[girl];
            if(m==0)
                engage(boy,girl);
            else
            {
                if(lover(boy,m,girl))
                    engage(boy,girl);
                else q.push(boy);
            }
        }
        for(int i=1;i<=n;i++)
        {
            int boy=to_boy[boy_name[i][0]-'a'];
            printf("%c %c\n",boy_name[i][0],girl_name[future_wife[boy]][0]);
        }
    }
    return 0;
}

HDOJ 1914 The Stable Marriage Problem

时间: 2024-11-06 23:18:23

HDOJ 1914 The Stable Marriage Problem的相关文章

【HDOJ】1914 The Stable Marriage Problem

稳定婚姻问题,Gale-Shapley算法可解. 1 /* 1914 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include &l

The Stable Marriage Problem

经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打分:我最爱a,其次爱b,再次爱c...每个帅哥打的分不同,你最爱的可能是我最讨厌的我最爱的可能是他不甚喜欢的.同样,每个美女也同样给每个帅哥打分.现在需要给他们搭配出100对新郎新娘,并且要保证所得到是稳定婚姻的搭配.那么,什么是不稳定的婚姻呢?所谓不稳婚姻是说, 比如说有两对夫妇(M1.F1)和(

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

题目给出n个男的和n个女的各自喜欢对方的程度,让你输出一个最佳搭配,使得他们所有人的婚姻都是稳定的. 所谓不稳婚姻是说,比如说有两对夫妇M1,F1和M2,F2,M1的老婆是F1,但他更爱F2;而F2的老公虽说是M2.但她更爱M1,这样的婚姻就是不稳婚姻,M1和F2理应结合,他们现在各自的婚姻都是错误的. 整个算法基于,男性轮流向女性求婚,每次求婚对象都是没有拒绝过自己且自己最喜欢的女性.而女性对于每个求婚者,若她是单身,则接受,否则,就看她更喜欢当前求婚者还是她的未婚夫,选择更好的那个. 这种执

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

转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序,同时每位男生也按照自己的偏爱程度将女生排序.然后将这n个女生和n个男生配成完备婚姻. 如果存在两位女生A和B,两位男生a和b,使得A和a结婚,B和b结婚,但是A更偏爱b而不是a,b更偏爱A而不是B,则这个婚姻就是不稳定的,A和b可能背着别人相

poj3487 &amp; hdu1914 The Stable Marriage Problem 稳定婚姻系统

//n个男生,n个女生配对 //n个男生在心中对每个女生有个排名 //n个女生在心中对每个男生有个排名 //问怎样配对使得所有人都配对好且不存在两对婚姻中存在 //A和a结婚,B和b结婚,但是A更偏爱b而非a而且b也更偏爱A而非B //这样A和b可能会私奔 //①这里用的是Gale-Shapley算法 //每一位单身男在所有尚未拒绝她的女士中选择一位被他排名最优先的女士: //②每一位女士将正在追求她的单身男与其当前男友进行比较,选择其中排名优 //先的男士作为其男友,即若单身男优于当前男友,则

Stable Matching Problem

The Stable Matching Problem originated, in part, in 1962, when David Gale and Lloyd Shapley, two mathematical economists, asked the question: Could one design a college admissions process, or a job recruiting process, that was self-enforcing? What di

HDOJ 4974 A simple water problem

A simple water problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 173    Accepted Submission(s): 112 Problem Description Dragon is watching competitions on TV. Every competition is held be

水题 HDOJ 4716 A Computer Graphics Problem

题目传送门 1 /* 2 水题:看见x是十的倍数就简单了 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <string> 9 #include <cmath> 10 using namespace std; 11 12 const int MAXN = 1e4 + 10; 13

hdoj 5349 MZL&#39;s simple problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5349 1 #include<stdio.h> 2 int main(){ 3 int cnt; 4 int max; 5 int N; 6 int ch; 7 while(~scanf("%d",&N)){ 8 cnt = 0; 9 while(N--){ 10 scanf("%d",&ch); 11 if(ch==1){ 12 scanf(