HDOJ 1914 The Stable Marriage Problem

来源:互联网 发布:微商吸粉软件 编辑:程序博客网 时间:2024/05/22 06:35

The Stable Marriage Problem

TimeLimit: 5000/1000 MS (Java/Others)    Memory Limit:65535/32768 K (Java/Others)
Total Submission(s): 881    Accepted Submission(s): 445

Problem Description

The stablemarriage problem consists of matching members of two different sets accordingto the member’s preferences for the other set’s members. The input for ourproblem 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 oppositegender 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 iscalled stable, if there is no pair (m, f) such that f ∈ F prefers m ∈ M to her current partner and m prefersf over his current partner. The stable marriage A is called male-optimal ifthere is no other stable marriage B, where any male matches a female he prefersmore than the one assigned in A.

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

 

 

Input

The first linegives you the number of tests. The first line of each test case containsinteger 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 nlines, that describe preferable lists for males. Next n lines describepreferable lists for females.

 

 

Output

For each testcase 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 theirmale 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


稳定婚姻入门模板题

#include <bits/stdc++.h>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define f(i,a,b) for(int i=(a);i<=(b);++i)#define ll long longconst int maxn = 35;const int mod = 475;const ll INF = 0x3f3f3f3f;const double eps = 1e-6;#define rush() int T;scanf("%d",&T);while(T--)int b[maxn][maxn],g[maxn][maxn],flag[maxn];int bm[maxn],gm[maxn],order[maxn];int n;int main(){    char s[maxn],str[maxn];    int t,u;    scanf("%d",&t);    while(t--)    {        queue<int>q;        scanf("%d",&n);        mst(bm,0),mst(gm,0);        mst(b,0),mst(g,0);        mst(flag,0);        f(i,1,n)        {            scanf("%s",str);            flag[str[0]-'a'+1]=1;        }        f(i,1,n)        {            scanf("%s",str);        }        f(i,1,n)        {            scanf("%s",s);            u=s[0]-'a'+1;            f(j,1,n)            {                b[u][j]=s[j+1]-'A'+1;            }            order[u]=1;            q.push(u);        }        f(i,1,n)        {            scanf("%s",s);            u=s[0]-'A'+1;            f(j,1,n)            {                g[u][s[j+1]-'a'+1]=j;            }        }        while(q.size())        {            int cur=q.front();            q.pop();            u=b[cur][order[cur]++];            if(!gm[u])            {                gm[u]=cur;                bm[cur]=u;            }            else if(g[u][cur]<g[u][gm[u]])            {                bm[gm[u]]=0;                q.push(gm[u]);                gm[u]=cur;                bm[cur]=u;            }            else            {                q.push(cur);            }        }        f(i,1,26)        {            if(flag[i])                printf("%c %c\n",i-1+'a',bm[i]-1+'A');        }        if(t)            printf("\n");    }    return 0;}

0 0
原创粉丝点击