<题目链接>

题目大意:

给你N个男生和N个女生,并且给出所有男生和女生对其它所有异性的喜欢程度,喜欢程度越高的两个异性越容易配对,现在求出它们之间的稳定匹配。

解题分析:

稳定婚姻问题的模板题,需要用到Gale_Shapley算法,GS算法讲解  >>>

这个算法还是很直观的。

 #include <iostream>
#include <cstring>
#include <stack>
#include <string>
#include <map>
#include <algorithm>
using namespace std; #define N 505
#define clr(a,b) memset(a,b,sizeof(a))
#define rep(i,s,t) for(int i=s;i<=t;i++) int n,getmp_boy[N][N],getmp_girl[N][N],boy[N],girl[N],rnk[N];
map<string,int>mp_boy,mp_girl;
string s,name_boy[N],name_girl[N]; void Gale_Shapley(){
clr(boy,);clr(girl,);
rep(i,,n) rnk[i]=;
while(true){
bool flag=false;
rep(i,,n){
if(!boy[i]){
int x=getmp_boy[i][rnk[i]++]; //x为当前男生所最求的他没有尝试最求过的最喜欢的女生
if(!girl[x]){ //如果这个女生没有和男生配对
boy[i]=x; //那么这对男女进行配对
girl[x]=i;
}else if(getmp_girl[x][i] > getmp_girl[x][girl[x]]){ //如果当前女生已经配对,那么就判断她对这两个男生的喜欢程度
boy[girl[x]]=; //将原来的男生抛弃
girl[x]=i; //将这两个男女进行配对
boy[i]=x;
}
flag=true;
}
}
if(!flag)break; //如果所有男生都已配对,则直接退出
}
rep(i,,n) cout<<name_boy[i]<<" "<<name_girl[boy[i]]<<endl;
} int main(){
ios_base::sync_with_stdio(false);
cin.tie();cout.tie();
while(cin>>n){
mp_boy.clear();mp_girl.clear();
int pos=,tmp;
rep(i,,n){
cin>>s;name_boy[i]=s;
mp_boy[s]=i;
rep(j,,n){
cin>>s;tmp=mp_girl[s];
if(!tmp){ //对于之前没有出现过个的女生姓名,重新分配序号
tmp=pos++;
mp_girl[s]=tmp;
name_girl[tmp]=s;
}
getmp_boy[i][j]=tmp; //记录第i个男生第j个喜欢的女生是tmp
}
}
rep(i,,n){
cin>>s;int x=mp_girl[s];
rep(j,,n){
cin>>s;int y=mp_boy[s];
getmp_girl[x][y]=n-j; //记录第i个女生喜欢男生y的程度是n-j
}
}
Gale_Shapley();
}
}

2019-01-17

HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)的更多相关文章

  1. HDU 1522 Marriage is Stable 稳定婚姻匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1522 #include<bits/stdc++.h> #define INF 0x3f3f3f3f ...

  2. HDU1522 稳定婚姻匹配 模板

    Marriage is Stable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)

    Description The stable marriage problem consists of matching members of two different sets according ...

  4. 【HDU1914 The Stable Marriage Problem】稳定婚姻问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...

  5. HDU1914 稳定婚姻匹配

    The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (J ...

  6. HDU 1512 Monkey King(左偏树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1512 题意: 有n只猴子,每只猴子一开始有个力量值,并且互相不认识,现有每次有两只猴子要决斗,如果认识,就不打了 ...

  7. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  8. HDU 3068 最长回文( Manacher模板题 )

    链接:传送门 思路:Manacher模板题,寻找串中的最长回文子串 /***************************************************************** ...

  9. HDU - 3081 Marriage Match II 【二分匹配】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意 有n对男女 女生去选男朋友 如果女生从来没和那个男生吵架 那么那个男生就可以当她男朋友 女 ...

随机推荐

  1. Confluence 6 WebDAV 禁用严格路径检查

    如果你在你的 WebDAV 客户端发现了一些不正常的现象,例如文件夹在 Confluence 中是存在的,但是在你客户端下载的文件中就不存在了.你可以禁用  WebDAV 插件中的严格路径检查选项,这 ...

  2. canvas 保存bitmap到本地

    File f = new File("/sdcard/DCIM/Camera/0.png"); FileOutputStream fos = null; try { fos = n ...

  3. usrp使用

    首先打开linux 输入uhd_find_divice gqrx

  4. RFC2119:RFC协议动词含义

    协议地址:http://www.ietf.org/rfc/rfc2119.txt MUST 必须的.通过它描述的对象,是强制要求的.它与REQUIRED和SHALL含义相同. MUST NOT 不允许 ...

  5. 快速部署网络爬虫框架scrapy

    1. 安装Anaconda,因为Anaconda基本把所有需要依赖的环境都一键帮我们部署好了,不需要再操心其他事了,进官网选择需要下载的版本:https://www.anaconda.com/down ...

  6. I: Carryon的字符串排序(字典树/map映射)

    2297: Carryon的字符串 Time Limit: C/C++ 1 s      Java/Python 3 s      Memory Limit: 128 MB      Accepted ...

  7. CMD批处理——forfiles命令使用,自动删除过期备份文件

    公司服务器用来备份数据的硬盘过段时间就会被备份文件占满,弄得我老是要登录到服务器去手工删除那些老的文件,有时忘记了就会导致硬盘空间不足而无法备份.因为只要保留最近几天的备份,如果可以做一个批处理让系统 ...

  8. “Error:(1, 1) java: 非法字符: '\ufeff'”错误解决办法

    原因 用Windows记事本打开并修改.java文件保存后重新编译运行项目出现“Error:(1, 1) java: 非法字符: '\ufeff'”错误,如下图所示:     原来这是因为Window ...

  9. python---顺序查找,二分查找

    比较熟悉了. 但要注意细节, 二分查找时,普通方法mid处理,递归时,mid处理. # coding = utf-8 def sequential_search(a_list, item): pos ...

  10. UE4 ShooterGame Demo的开火的代码

    之前一直没搞懂按下鼠标左键开火之后,代码的逻辑是怎么走的,今天看懂了之前没看懂的部分,进了一步 ShooterCharacter.cpp void AShooterCharacter::OnStart ...