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

    题面

    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

    思路

    裸题不说了,但我又忍不住吐槽HDU的评测机,在题库交的PE代码在专题训练里面可以ac,专题训练里面ac的代码,题库里PE,呵呵~

    代码实现

    #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 0x3f3f3f3f3f3f3f3f
    #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; }
    inline int read() {
        char ch=getchar(); int x=0, f=1;
        while(ch<'0'||ch>'9') {
            if(ch=='-') f = -1;
            ch=getchar();
        } 
        while('0'<=ch&&ch<='9') {
            x=x*10+ch-'0';
            ch=getchar();
        }   return x*f;
    }
    const int maxn=30;
    int t,n;
    string s;
    int cnt;
    char a[maxn],b[maxn];
    map <char,int > mp_mm,mp_gg;
    int gg[maxn][maxn],mm[maxn][maxn];
    int gg_match[maxn],mm_match[maxn];
    int p[maxn];
    bool sign=0;
    
    void Gs () {
       bool flag=1;
       MT (gg_match,-1);
       MT (mm_match,-1);
       MT (p,0);
       while (flag) {
          flag=0;
          rep (i,1,n) {
              if (gg_match[i]==-1&&p[i]<=n) {
                  int c=gg[i][++p[i]];
                  if (mm_match[c]==-1) {
                      mm_match[c]=i;
                      gg_match[i]=c;
                  }
                  else if (mm[c][i]<mm[c][mm_match[c]]) {
                      gg_match[mm_match[c]]=-1;
                      gg_match[i]=c;
                      mm_match[c]=i;
                  }
                  flag=1;
              }
          }
       }
    }
    
    void countrank () {
       rep (i,1,n) {
           cin>>s;
           cnt=0;
           int now=mp_gg[s[0]];
           rep (j,2,s.size ()-1) {
               int girl=mp_mm[s[j]];
               gg[now][++cnt]=girl;
           }
       }
       rep (i,1,n) {
           cin>>s;
           cnt=0;
           int now=mp_mm[s[0]];
           rep (j,2,s.size ()-1) {
               int boy=mp_gg[s[j]];
               mm[now][boy]=++cnt;
           }
       }
    }
    
    void solve () {
       cin>>n;
       cnt=0;
       mp_mm.clear ();
       mp_gg.clear ();
       rep (i,1,n) {
           cin>>a[i];
           if (mp_gg[a[i]]==0) {
              mp_gg[a[i]]=(++cnt);
           }
       }
       cnt=0;
       rep (i,1,n) {
           cin>>b[i];
           if (mp_mm[b[i]]==0) {
               mp_mm[b[i]]=(++cnt);
    
           }
       }
       countrank ();
       Gs ();
    //    rep (i,1,n) { 
    //     rep (j,1,n) {
    //         cout<<gg[i][j]<<" ";
    //     }
    //     cout<<endl;
    //    }
       rep (i,1,n) {
           cout<<a[i]<<" "<<b[gg_match[i]]<<endl;
       }
    }
    
    int main () {
       cin>>t;
       int ca=0;
       while (t--) {
           solve ();
           printf ("
    ");
       }
        return 0;
    }
    
    
  • 相关阅读:
    [mysql] 删除唯一约束unique
    onethink 路由规则无效问题解决
    mysql source 乱码
    NLPIR
    词性标记集--计算所汉语
    [thinkphp] 无限极分类
    UITableViewCell在非Nib及Cell重用下设置CellStyle
    UIViewController的初始化
    转:NSString / NSData / char* 类型之间的转换
    转:苹果Xcode帮助文档阅读指南
  • 原文地址:https://www.cnblogs.com/hhlya/p/13414252.html
Copyright © 2011-2022 走看看