zoukankan      html  css  js  c++  java
  • HDU Marriage is Stable (稳定婚姻匹配)

    题目

    Albert, Brad, Chuck are happy bachelors who are in love with Laura, Marcy, Nancy. They all have three choices. But in fact, they do have some preference in mind. Say Albert, 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 thinks Nancy 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 Chuck instead of Albert. And what's more, Chuck likes Laura better than Nancy. So Laura 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 any boy G and girl M, with their choice m[G] and m[M], it will not happen that rank(G, M) < rank(G, m[G])and rank(M, G) < rank(M, m[M]).

    Input
    Each case starts with an integer n (1 <= n <= 500), the number of matches to make. The following n lines contain n + 1 names each, the first being name of the boy, 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
    If there is a stable marriage, print n lines with two names on each line. You can choose any one if there are multiple solution. Print "Impossible" otherwise. Print a blank line after each test.

    Sample Input
    3
    Albert Laura Nancy Marcy
    Brad Marcy Nancy Laura
    Chuck Laura Marcy Nancy
    Laura Chuck Albert Brad
    Marcy Albert Chuck Brad
    Nancy Brad Albert Chuck

    Sample Output
    Albert Nancy
    Brad Marcy
    Chuck Laura

    思路

    稳定婚姻,用map转换一下字符串就方便操作了;

    代码实现

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=0;i<end;i++)
    #define inf 0x3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    typedef pair<int ,int> PII;
    ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
    map <string ,int > mp_mm,mp_gg;
    const int maxn=505;
    int gg[maxn][maxn],mm[maxn][maxn];
    int gg_match[maxn],mm_match[maxn];
    string ss[maxn],str;
    int p[maxn];
    int n;
    
    
    void input () {
        mp_mm.clear ();
        mp_gg.clear ();
        int cnt=0;
        rep (i,1,n) {
            cin>>ss[i];
            mp_gg[ss[i]]=i;
            rep (j,1,n) {
               cin>>str;
               if (!mp_mm[str])  {
                   mp_mm[str]=++cnt;
                   ss[cnt+n]=str;
               }
               gg[i][j]=mp_mm[str];
            }
        }
    
        rep (i,1,n) {
            cin>>str;
            int a=mp_mm[str];
            rep (j,1,n) {
                cin>>str;
                int b=mp_gg[str];
                mm[a][b]=j;
            }
        }
    
    }
    
    void solve () {
        bool flag=1;
        MT (p,1);
        MT (mm_match,-1);
        MT (gg_match,-1);
         
        while (flag) {
            flag=0;
            rep (i,1,n) {
                if (gg_match[i]==-1&&p[i]<=n) {
                    int a=gg[i][p[i]++];
                    if (mm_match[a]==-1) {
                        mm_match[a]=i;
                        gg_match[i]=a;
                    }
                    else if (mm[a][i]<mm[a][mm_match[a]]) {
                        gg_match[mm_match[a]]=-1;
                        mm_match[a]=i;
                        gg_match[i]=a;
                    }
                    flag=1;
                }
            }
    
        }
    }
    
    int main () {
        while (cin>>n) {
           input ();
           solve ();
           rep (i,1,n) {
               cout<<ss[i]<<" "<<ss[gg_match[i]+n]<<endl;
           }
           cout<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    Android仿人人客户端(v5.7.1)——个人主页(三)
    hdu2554-N对数的排列问题
    POJ1363:Rails
    golang printf
    HDU1200:To and Fro
    [C# 基础知识系列]专题六:泛型基础篇——为什么引入泛型
    poj 2480 (欧拉函数应用)
    Re:从0开始的微服务架构--(二)快速快速体验微服务架构?--转
    爬虫推荐的工具
    python2 与 python3 语法区别--转
  • 原文地址:https://www.cnblogs.com/hhlya/p/13399895.html
Copyright © 2011-2022 走看看