UVALive 3989Ladies' Choice(稳定婚姻问题)
题意:n个男生和女生,先是n行n个数,表示每一个女生对男生的好感值排序,然后是n行n列式每一个男生的好感值排序,输出N行,即每个女生在最好情况下的男生的编号
分析:如果是求女生的最好情况下,就要从女生开始选,这样女生都是从最好的到不好的来选,而男生却相反--只能娶那些自己有可能最没好感的女生,因为男生是被动的,他最喜欢的女生不见的会向他求婚。
刘汝佳书上命名错了,so也跟着把男生当成女生了,懒得改命名了,
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;
const int Max = ;
int pref[Max][Max], order[Max][Max], Next[Max];
int future_hasband[Max], future_wife[Max];
queue <int> q; //单身女生队列
void engage(int man, int woman)
{
int m = future_hasband[woman];
if(m)
{
future_wife[m] = ;
q.push(m);
}
future_hasband[woman] = man;
future_wife[man] = woman;
return;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
while(!q.empty())
q.pop();
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
scanf("%d", &pref[i][j]);
future_wife[i] = ; // 女生i没有匹配
Next[i] = ; // 都是从第一个开始查找
q.push(i); // 加入单身队列
}
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)
{
int x;
scanf("%d", &x);
order[i][x] = j; // 表示男生i 对 女生 x 的好感等级
}
future_hasband[i] = ;
}
while(!q.empty())
{
int man = q.front();
q.pop();
int woman = pref[man][Next[man]++];
if(future_hasband[woman] == ) // 表示这个男生并没有 匹配
engage(man, woman);
else if(order[woman][man] < order[woman][future_hasband[woman]]) // 虽然已经匹配了,但是同已经匹配的好感层度来看,更喜欢这个
engage(man, woman);
else
q.push(man); // 否则继续单身,等待下次解救
}
for(int i = ; i <= n; i++)
printf("%d\n", future_wife[i]);
if(T)
printf("\n");
} return ;
}
UVALive 3989Ladies' Choice(稳定婚姻问题)的更多相关文章
- 训练指南 UVALive - 3989(稳定婚姻问题)
ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- LA 3989 - Ladies' Choice 稳定婚姻问题
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法
题目链接:https://vjudge.net/problem/UVALive-3989 题解: 题意:有n个男生和n个女生.每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行. ...
- Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- UVALive-3989 Ladies' Choice (稳定婚姻问题)
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
- HDU1522 稳定婚姻匹配 模板
Marriage is Stable Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...
随机推荐
- Bootstrap系列 -- 8. 代码显示
一. Bootstrap中的代码块 代码块一般在博客中使用的较多,比较博客园中提供的贴代码. 在Bootstrap中提供了三种形式的代码显示 1. 使用<code></code> ...
- [BZOJ1579][Usaco2009 Feb]Revamping Trails 道路升级(二维最短路问题)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1579 分析: 设d[i][j]表示从1走到i.改了j条边的最短路径长度 如果设i相连的 ...
- cxf和jaxws的对比
和jaxws相比,服务器发布方式和客户端访问方式不同 以下是cxf的代码: 服务器发布方式: package service; import javax.xml.ws.Endpoint; import ...
- ClassLoader 详解及用途
ClassLoader主要对类的请求提供服务,当JVM需要某类时,它根据名称向ClassLoader要求这个类,然后由ClassLoader返回这个类的class对象. 1.1 几个相关概念Class ...
- 如何用linux命令查看nginx是否在正常运行
有时想知道nigix是否在正常运行,需要用linux命令查看nginx运行情况. 执行命令: ps -A | grep nginx 如果返回结果的话,说明有nginx在运行,服务已经启动. 如果 ...
- Ubuntu 14.04 安装最新稳定版Nginx 1.6.0
如果已经安装,请先卸载sudo apt-get remove nginx最新的稳定版Nginx 1.6.0在ubuntuupdates ppa库中提供,网址http://www.ubuntuupdat ...
- 【BZOJ 4269】再见Xor
zky学长提供的线性基求法: for(int i=1;i<=n;i++) for(int j=64;j>=1;j--) { if(a[i]>>(j-1)&1) { if ...
- css 导航,菜单对应页面切换效果实现方法
实现原理: 每个菜单有多个li标签,每个li标签含一个id,li标签的id用来标记:点击效果 每个页面有一个id,这个id的作用是对应每个li标签的点击链接对应的页面,它的作用是用来标记:li标签的h ...
- javascript 红宝书笔记之函数、变量、参数、作用域
ECMAScript 不介意传进来多少个参数,也不介意传进来的参数类型. 理解参数: 命名的参数只提供便利,不是必需的. ECMAScript 的变量包含两种不同的数据类型的 ...
- python之旅3
1 collections系列 方法如下 class Counter(dict): '''Dict subclass for counting hashable items. Sometimes ca ...