UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意:
给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起。为了杜绝这种现象,求每个男的最后所搭配的女伴是谁。
思路:
怎么感觉题意有点问题,输出的是第i行的男人所搭配的女人,即输出的是女人!
每个男人依次选择最喜欢的女人求婚,若同一个女的没人抢,则临时抢亲成功,若需要抢,明显女的都是喜欢挑长腿oba啦,矮冬瓜自动加入单身狗队列继续求婚!当单身狗所求的女神已订婚,只要女神更爱单身狗,没有什么可以阻挡他们,女神果断抛弃未婚夫,未婚夫加入单身狗队列。直至地球上没有单身狗为止!
复杂度O(n2),每个男的最多只会向n个女的都求婚一次,而女的没有选择权,只有接受和拒绝。著名的Gale-Shapley算法。
#include <bits/stdc++.h>
#define max(X,Y) ((X) > (Y) ? (X) : (Y))
#define min(X,Y) ((X) < (Y) ? (X) : (Y))
#define pii pair<int,int>
#define INF 0x7f7f7f7f
#define LL long long
using namespace std;
const int N=; deque<int> que[N]; //心目中的女神排名队列
int girl[N], boy[N];
int imp[N][N]; int cal(int n)
{
memset(girl, , sizeof(girl) );
deque<int> single;
for(int i=; i<=n; i++) single.push_back(i); while(!single.empty())
{
int x=single.front(); single.pop_front(); //取出单身狗
int op=que[x].front(); que[x].pop_front(); //取出x的暗恋对象 if(!girl[op]) girl[op]=x,boy[x]=op; //女神初恋
else if( imp[op][x] < imp[op][girl[op]] ) //女神抛弃现任
{
single.push_back(girl[op]);
girl[op]=x;
boy[x]=op;
}
else single.push_back(x); //单身狗被拒绝
}
for(int i=; i<=n; i++) printf("%d\n",boy[i]);
} int main()
{
//freopen("input.txt", "r", stdin);
int t, n, b, j=;
cin>>t;
while(t--)
{
if(j) printf("\n");
j++;
scanf("%d", &n);
for(int i=; i<=n; i++)
{
que[i].clear();
for(int j=; j<=n; j++)
{
scanf("%d",&b);
que[i].push_back(b);
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&b);
imp[i][b]=j; //b对于i来说,排在第几。
}
} cal(n);
}
return ;
}
AC代码
UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)的更多相关文章
- UVA 1175 - Ladies' Choice
1175 - Ladies' Choice 链接 稳定婚姻问题. 代码: #include<bits/stdc++.h> using namespace std; typedef long ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- 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(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
- 【LA 3989 训练指南】女士的选择 【稳定婚姻问题】
我们先来学一下稳定婚姻问题 什么是稳定婚姻问题? 有n个女士和n个男士,他们要一一进行配对.每个男士心中对这n个女士都有一个排名,同理,每个女士心里对n个男性也有一个排名.我们要做的是,在他们配对完成 ...
- LA 3989 - Ladies' Choice 稳定婚姻问题
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive-3989 Ladies' Choice (稳定婚姻问题)
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
- UVALive 3989 Ladies' Choice
Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
随机推荐
- POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- UIFont 字体样式 [UIFont fontWithName~];
设置字体样式代码:[UIFont fontWithName:@"Arial-BoldMT" size:15] 下边一一列举: 一:Font Family: American Typ ...
- [Selenium] The most commonly used CSSSelector
CSSSelector Example Description element.element div.dropdown Select all <div> elements whose ...
- table中tr或者td的点击事件
直接把时间添加到table或者tbody上,只有下面的tr或者td才能促发事件,通过e.target可以获得当前点击tr或者td,这样就可以进行查询或者删除操作了 如果是删除,直接e.target.r ...
- uoj103 apio2014 Palindromes
题目链接:http://uoj.ac/problem/103 题解: 首先,我们可以用后缀自动机算出每个字符串的出现次数.然后我们可以用manacher找出所有不同的回文串(o(n)个),统计答案即可 ...
- 【旧文章搬运】从PEB获取内存中模块列表
原文发表于百度空间,2008-7-25========================================================================== PEB中的L ...
- Divide the Sequence
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...
- View Controller Programming Guide for iOS---(五)---Resource Management in View Controllers
Resource Management in View Controllers View controllers are an essential part of managing your app’ ...
- node-- express()模块
1.代码分析 var express = require('express'); var app = express(); app.get('/', function(req, res){ res.s ...
- Moctf--没时间解释了
记录一道简单的题目. 打开后就张这个样子,,然后看到url为index2.php---->所以我们把它改为index.php(用burp抓包才行,这是一个302跳转). 看到它提示我们要uplo ...