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个男生,每位女生按照她的偏爱程度将男生排序, ...
随机推荐
- Scala之OOP
/** * 1,在Scala中定义类是用class关键字: * 2,可以使用new ClassName的方式构建出类的对象: * 3, 如果名称相同,则object中的内容都是class的静态内容,也 ...
- 化茧成蝶,开源NetWorkSocket通讯组件
前言 前后历时三年,期间大量参考.Net Framework和Asp.net MVC源代码,写写删删再重构,组件如今更新到V1.5.x了.从原来的丑小鸭,变成今天拥有稳定和强大的tcp协议支持基础层, ...
- Matlab 的reshape函数
看Matlab的help文档讲得不是清楚. 先给上一段代码: >> a=[1 2 3;4 5 6;7 8 9;10 11 12]; >> b=reshape(a,2,6); 这 ...
- directly receive json data from javascript in mvc
if you send json data to mvc,how can you receive them and parse them more simply? you can do it like ...
- CSS3自动添加省略号
text-overflow:ellipsis; white-space:nowrap; overflow:hidden; 不换行,一行显示溢出时,文本自动换行.以前都是js计算的,现在可好. elli ...
- import random 模块导入
import random print(random.random()) #浮点数值 print(random.randint(1,2))#循环显示1,2 print(random.randrange ...
- 线性表的顺序存储结构C语言版
#include <stdio.h> #define MAXSIZE 101 #define N 10 typedef struct SeqList { int data[MAXSIZE] ...
- 【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...
- java 中hashmap和hashtable的区别
1.hashmap是线程不安全的,能够有key,value能有null hashtable是线程安全的,key,value不能有null
- 强连通 HDU3072
n个点m条边 m条边 权值 简单点说就是求把所有强连通分量连在一起所需的最小花费 不用双向 图是联通的 cost[] 维护到这里的最小花费求和 #include<stdio.h> #in ...