acm专题5——稳定婚配问题

编程入门 行业动态 更新时间:2024-10-24 00:22:25

acm专题5——<a href=https://www.elefans.com/category/jswz/34/1767933.html style=稳定婚配问题"/>

acm专题5——稳定婚配问题

模板

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int MAXN=505;
int n,bf,gf;
map<int,string>b;
map<int,string>g;
map<string,int>b1;
map<string,int>g1;
int rank1[MAXN];
int match_boy[MAXN],match_girl[MAXN];//match_boy[i]编号为i的男士配对的女士
int boy[MAXN][MAXN],girl[MAXN][MAXN];//boy[i][j] 第i个男的的第j个喜欢的女的的编号,girl[i][j]在编号为i的女士心中,编号为j的男士的排名
void init()
{memset(match_boy,0,sizeof(match_boy));memset(match_girl,0,sizeof(match_girl));memset(rank1,0,sizeof(rank1));b.clear();g.clear();b1.clear();g1.clear();bf=gf=0;
}
void match()
{int flag=1;while(flag){flag=0;for(int i=1;i<=n;i++){if(match_boy[i]==0){int temp=boy[i][++rank1[i]];if(match_girl[temp]==0)match_boy[i]=temp,match_girl[temp]=i;else if(girl[temp][i]<girl[temp][match_girl[temp]] ){match_boy[match_girl[temp]]=0;match_boy[i]=temp;match_girl[temp]=i;}flag=1;}}}
}
char name[MAXN];
string na;
void solve()
{while(~scanf("%d",&n))
{for(int i=1;i<=n;i++){scanf("%s",name);na=string(name);b[++bf]=na;b1[na]=bf;for(int j=1;j<=n;j++){scanf("%s",name);na=string(name);if(i==1)g[++gf]=na,g1[na]=gf;boy[i][j]=g1[na];}}for(int i=1;i<=n;i++){scanf("%s",name);na=string(name);int gn=g1[na];for(int j=1;j<=n;j++){scanf("%s",name);na=string(name);girl[gn][b1[na]]=j;}}match();for(int i=1;i<=n;i++)cout<<b[i]<<' '<<g[match_boy[i]]<<endl;puts("");init();
}return;
}
int main()
{
/*ll T;scanf("%lld",&T);while(T--)
*/solve();return 0;
}

更多推荐

acm专题5——稳定婚配问题

本文发布于:2024-02-26 10:43:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1702215.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:稳定   专题   acm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!