<题目链接>

题目大意:

给你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. swift 学习- 14 -- 继承

    // 一个类可以继承另一个 类的方法, 属性和其他特征, 当一个类继承其他类时, 继承类叫子类, 被继承类叫父类 或 (超类), 在 Swift 中, 继承是区分 [类] 和其他类型的 一个基本特征 ...

  2. 通过设置ie的通过跨域访问数据源,来访问本地服务

    1.首先设置通过域访问数据源 设置通过域访问数据源 2.javascript脚本ajax使用本地服务登录(评价,人证的类似)接口 <html> <head> <scrip ...

  3. Confluence 6 访问日志脚本

    日志访问脚本在连接:https://confluence.atlassian.com/download/attachments/133267635/Atlassian-accessLogScripts ...

  4. Confluence 6 的系统配置信息的示例

    awt.toolkit sun.awt.X11.XToolkit file.encoding.pkg sun.io java.specification.version 1.8 sun.cpu.isa ...

  5. 树形dp 入门

    今天学了树形dp,发现树形dp就是入门难一些,于是好心的我便立志要发一篇树形dp入门的博客了. 树形dp的概念什么的,相信大家都已经明白,这里就不再多说.直接上例题. 一.常规树形DP P1352 没 ...

  6. GitHub搭配使用Travis CI 进行自动构建服务

    Travis CI (Continuous Integration)持续集成服务 用处:自动监控软件仓库,可以在代码提交后立刻执行自动测试或构建 1.在Github自己的仓库根目录里添加.travis ...

  7. 【python】threadpool的内存占用问题

    先说结论: 在使用多线程时,不要使用threadpool,应该使用threading, 尤其是数据量大的情况.因为threadpool会导致严重的内存占用问题! 对比threading和threadp ...

  8. FFmpeg的一般流程

    FFMPeg一般流程: 1.av_register_all();//注册所有文件格式和编解码库 2.avformat_network_init();//打开网络视频流 3.av_open_input_ ...

  9. 眼底血管分割训练函数(SVM,Adaboost)

    # -*- coding: utf-8 -*- import numpy as np from sklearn import svm from sklearn.model_selection impo ...

  10. 去掉A标签的点击选中边框

    非IE a:focus { outline:none; }