Regionals 2007 >> Europe - Southwestern Ladies' Choice 稳定婚姻问题 uva live 3989

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=22&page=show_problem&problem=1990


这个算法很经典 ,不过我不会证明,可以看看百度百科,讲得很清楚,数学家们已经证明过了,我们直接用就行。

对于这个算法,总是对男性有利,在稳定婚姻的前提下,每个男性都尽可能选到自己最喜欢的女性。

而女性则不然。

百度百科:

http://baike.baidu.com/link?url=JX0TfZPhof0KFxKBJMT8y1BBpccAaRthSH2xVCfthCoU9a0CITgB4HpXNvd_sYzt6BU68NN2rKheq9hy6rZLSa


这个题目是要让女性有最好的选择,直接男性当作"算法里的女性"处理,女性当作"算法里的男性"处理 就行了。

最后输出 "算法里的男性"选择结果。


#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second

using namespace std;
//const int INF=    ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn=1000+10    ;
//const int maxm=    ;
//by yskysker123
int pre[maxn][maxn];
int wife[maxn];
int husband[maxn];
int order[maxn][maxn];
int nex[maxn];
queue<int >q;
int n;
void engage(int man ,int woman)
{
    int x=husband[woman];

    if(x) wife[x]=0,q.push(x);//掉了q.push(x)!!!!!
    husband[woman]=man;
    wife[man]=woman;

}

void BFS()
{
    while(!q.empty())
    {
        int man=q.front();q.pop();
        int woman=pre[man][nex[man]++];//写成了int woman=nex[man]++;
        if(!husband[woman]) engage(man,woman);
        else if(  order[woman][man]< order[woman][husband[woman]]   )
            engage(man,woman);
        else q.push(man);

    }


}

int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            wife[i]=0;
            for(int j=1;j<=n;j++)
           {
                scanf("%d",&pre[i][j]);
           }
           q.push(i);
           nex[i]=1;


        }
        int x;
        for(int i=1;i<=n;i++)
        {
            husband[i]=0;
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&x);
                order[i][x]=j;
            }
        }
        BFS();
        for(int i=1;i<=n;i++)
        {
            printf("%d\n",wife[i]);
        }
        if(T>0)  putchar('\n');


    }


    return 0;
}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值