HDOJ 1522 Marriage is Stable

Marriage is Stable

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:65536/32768 K (Java/Others)
Total Submission(s): 1173    Accepted Submission(s): 624
Special Judge

Problem Description

Albert,Brad, Chuck are happy bachelors who are in love with Laura, Marcy, Nancy. Theyall have three choices. But in fact, they do have some preference in mind. SayAlbert, he likes Laura best, but that doesn't necesarily mean Laura likes him.Laura likes Chuck more than Albert. So if Albert can't marry Laura, he thinksNancy a sensible choice. For Albert, he orders the girls Laura > Nancy >Marcy.

For the boys:

Albert: Laura > Nancy > Marcy
Brad: Marcy > Nancy > Laura
Chuck: Laura > Marcy > Nancy

For the girls:

Laura: Chuck > Albert > Brad
Marcy: Albert > Chuck > Brad
Nancy: Brad > Albert > Chuck

But if they were matched randomly, such as

Albert <-> Laura
Brad <-> Marcy
Chuck <-> Nancy

they would soon discover it's not a nice solution. For Laura, she likes Chuckinstead of Albert. And what's more, Chuck likes Laura better than Nancy. SoLaura and Chuck are likely to come together, leaving poor Albert and Nancy.

Now it's your turn to find a stable marriage. A stable marriage means for anyboy G and girl M, with their choice m[G] and m[M], it will not happen thatrank(G, M) < rank(G, m[G]
and rank(M, G) < rank(M, m[M]).

 

 

Input

Eachcase starts with an integer n (1 <= n <= 500), the number of matches tomake.

The following n lines contain n + 1 names each, the first being name of theboy, and rest being the rank of the girls.

The following n lines are the same information for the girls.

Process to the end of file.

 

 

Output

Ifthere is a stable marriage, print n lines with two names on each line. You canchoose any one if there are multiple solution. Print "Impossible"otherwise.

Print a blank line after each test.

 

 

Sample Input

3

Albert LauraNancy Marcy

Brad Marcy NancyLaura

Chuck Laura MarcyNancy

Laura ChuckAlbert Brad

Marcy AlbertChuck Brad

Nancy Brad AlbertChuck

 

 

Sample Output

Albert Nancy

Brad Marcy

Chuck Laura


本质和前一道题完全一样,用map处理即可

#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 long
const int maxn = 505;
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 str1[maxn][10],str2[maxn][10],s1[10],s2[10];
    int u;
    while(~scanf("%d",&n))
    {
        map<string,int>mpb,mpg;
        queue<int>q;
        mst(bm,0),mst(gm,0);
        mst(b,0),mst(g,0);
        int tb=1,tg=1;
        f(i,1,n)
        {
            scanf("%s",str1[i]);
            mpb[str1[i]]=tb++;
            f(j,1,n)
            {
                scanf("%s",s1);
                if(!mpg[s1])
                {
                    strcpy(str2[tg],s1);
                    mpg[s1]=tg++;
                }
                b[mpb[str1[i]]][j]=mpg[s1];
            }
            order[tb-1]=1;
            q.push(tb-1);
        }
        f(i,1,n)
        {
            scanf("%s",s1);
            f(j,1,n)
            {
                scanf("%s",s2);
                g[mpg[s1]][mpb[s2]]=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,n)
        {
            printf("%s %s\n",str1[i],str2[bm[i]]);
        }
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值