题目链接

题意: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(稳定婚姻问题)的更多相关文章

  1. 训练指南 UVALive - 3989(稳定婚姻问题)

    ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...

  2. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  3. LA 3989 - Ladies' Choice 稳定婚姻问题

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  4. UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法

    题目链接:https://vjudge.net/problem/UVALive-3989 题解: 题意:有n个男生和n个女生.每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行. ...

  5. Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法

    /** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...

  6. 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)

    Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...

  7. UVALive-3989 Ladies' Choice (稳定婚姻问题)

    题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...

  8. HDU1522 稳定婚姻匹配 模板

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

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

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

随机推荐

  1. 探究JVM——垃圾回收

    垃圾回收主要考虑三件事情:哪些内存需要回收?什么时候回收?如何回收? 一.哪些内存需要回收? 堆内存:对于JVM 来说,垃圾回收主要是针对堆内存中的对象实例. 方法区:垃圾收集行为在方法区是比较少出现 ...

  2. WCF x509证书安装问题汇总

    1.win7及以后系统可以使用certlm.msc打开localmachine的证书管理界面.win7前的版本需要使用mmc打开控制台,然后选择文件->添加删除管理单元->选择证书.2.部 ...

  3. 7z压缩文件时排除指定的文件

    分享一个7z压缩文件时排除指定文件类型的命令行,感觉很有用: 7z a -t7z d:\updateCRM.7z d:\updateCRM\*.* -r -x!*.log -x!*bak a:创建压缩 ...

  4. 生成短链(网址) ShortUrlLink

    建表 CREATE TABLE [dbo].[ShortUrl]( [Id] [,) NOT NULL, [LongUrl] [nvarchar]() NOT NULL, [BaseUri] [int ...

  5. B树和B+树

    当数据量大时,我们如果用二叉树来存储的会导致树的高度太高,从而造成磁盘IO过于频繁,进而导致查询效率下降.因此采用B树来解决大数据存储的问题,很多数据库中都是采用B树或者B+树来进行存储的.其目的就是 ...

  6. 调用天气Api实现天气查询

    上面是简单截图: 前台代码: @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name=&qu ...

  7. mybatis resultMap映射学习笔记

    这几天,百度mybatis突然看不到官网了,不知道百度怎么整的.特此贴出mybatis中文官网: http://www.mybatis.org/mybatis-3/zh/index.html 一个学习 ...

  8. Beta版本冲刺——day1

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 站立式会议 培侨走的第4天,想他~( ...

  9. oracle操作记录

    由于之前建的job过多,造成数据库cpu占用率达到99%,造成需要的job崩溃. 以下为解决方案: 1. 查询当前的job列表 : select * from user_jobs; 2. 暂停所有的j ...

  10. Django form 中文提交 错误

    在文件头部添加 刻解决 import sys reload(sys) sys.setdefaultencoding("utf-8")