zoukankan      html  css  js  c++  java
  • 10.1.2 Marriage is Stable

    稳定婚配~~~~~~

    我是看白书看懂的,当然网上的资料也很好~

    http://blog.sina.com.cn/s/blog_5f48a0a10101150v.html

    http://hi.baidu.com/acmdearway/item/745d54c8160d3dd3964452e9

    http://en.wikipedia.org/wiki/Stable_marriage_problem

    简单来说,就是每个男生向自己喜欢的女生表白,然后每个女生从中选择自己认为最好的男生,然后接受这个男生,拒绝其他男生,那么被拒绝的那些男生再重复以上过程,可以证明一定存在合法解,我不得不说读入输出太蛋疼了,主程序还是很好写的

    Marriage is Stable

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 35 Accepted Submission(s): 26

    Problem Description
    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
     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<fstream>
     6 #include<sstream>
     7 #include<bitset>
     8 #include<vector>
     9 #include<string>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<stack>
    13 #include<queue>
    14 #include<stack>
    15 #include<map>
    16 #include<set>
    17 #define FF(i, a, b) for(int i=a; i<b; i++)
    18 #define FD(i, a, b) for(int i=a; i>=b; i--)
    19 #define REP(i, n) for(int i=0; i<n; i++)
    20 #define CLR(a, b) memset(a, b, sizeof(a))
    21 #define debug puts("**debug**")
    22 #define LL long long
    23 #define PB push_back
    24 using namespace std;
    25 
    26 const int maxn = 510;
    27 int pref[maxn][maxn], order[maxn][maxn], next[maxn];
    28 int hus[maxn], wife[maxn];
    29 queue<int> q;
    30 
    31 void enage(int man, int woman)
    32 {
    33     int m = hus[woman];
    34     if(m)
    35     {
    36         q.push(m);
    37     }
    38     wife[man] = woman;
    39     hus[woman] = man;
    40 }
    41 
    42 map<string, int> wo, ma;
    43 map<int, string> ans1, ans2;
    44 int man_cnt, woman_cnt, n;
    45 int man_id(string a)
    46 {
    47     if(!ma.count(a)) ma[a] = man_cnt++, ans1[man_cnt-1] = a;
    48     return ma[a];
    49 }
    50 int woman_id(string a)
    51 {
    52     if(!wo.count(a)) wo[a] = woman_cnt++, ans2[woman_cnt-1] = a;
    53     return wo[a];
    54 }
    55 
    56 int main()
    57 {
    58 freopen("1012.in","r",stdin);
    59 //freopen("test.out","w",stdout);
    60     while(~scanf("%d", &n))
    61     {
    62         man_cnt = woman_cnt = 1;
    63         wo.clear(), ma.clear(), ans1.clear(), ans2.clear();
    64         char a[111];
    65         int x, y;
    66         FF(i, 1, n+1)
    67         {
    68             scanf("%s", a); x = man_id(string(a));
    69             FF(j, 1, n+1)
    70             {
    71                 scanf("%s", a); y = woman_id(string(a));
    72                 pref[x][j] = y;
    73             }
    74             next[i] = 1;
    75             wife[i] = 0;
    76             q.push(i);
    77         }
    78         FF(i, 1, n+1)
    79         {
    80             scanf("%s", a); x = woman_id(string(a));
    81             FF(j, 1, n+1)
    82             {
    83                 scanf("%s", a); y = man_id(string(a));
    84                 order[x][y] = (int)1E9-j;
    85             }
    86             hus[i] = 0;
    87         }
    88         while(!q.empty())
    89         {
    90             int man = q.front(); q.pop();
    91             int woman = pref[man][next[man]++];
    92             if(order[woman][man] > order[woman][hus[woman]]) enage(man, woman);
    93             else q.push(man);
    94         }
    95         FF(i, 1, n+1) cout<<ans1[i]<<" "<<ans2[wife[i]]<<endl;
    96         puts("");
    97         while(!q.empty()) q.pop();
    98     }
    99 }
  • 相关阅读:
    青云黄允松:2016年是云计算市场最关键的一年
    知道创宇CTO杨冀龙:网络安全人才决定行业格局
    su 与 su
    如何进入单用户模式(CentOS6.9)
    在虚拟机中还原GHO镜像系统
    安装CentOS 7 文字版
    把typora改为微软雅黑+Consolas
    使用python操作文件实现购物车程序
    使用python操作json文本文件
    资源下载网址集合
  • 原文地址:https://www.cnblogs.com/cssystem/p/3335151.html
Copyright © 2011-2022 走看看