UVA 1175 - Ladies' Choice
1175 - Ladies' Choice
稳定婚姻问题。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for (;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ; int pref[N][N],order[N][N],Boys[N],Girls[N],cur[N];
queue<int>q; // 求婚队列 void Link(int u,int v) {
int t = Girls[v];
if (t) {
Boys[t] = ;
q.push(t);
}
Boys[u] = v;
Girls[v] = u;
} void solve() {
int n = read();
for (int i=; i<=n; ++i) {
for (int j=; j<=n; ++j)
pref[i][j] = read(); // 男孩心中第j个喜欢的女孩
cur[i] = ; // 求婚对象
Boys[i] = ; // 男孩的
q.push(i);
}
for (int i=; i<=n; ++i) {
for (int j=; j<=n; ++j) {
int t = read();
order[i][t] = j; // 男孩在女孩中的排名
}
Girls[i] = ; // 女孩的
}
while (!q.empty()) {
int u = q.front();q.pop();
int v = pref[u][cur[u]++];
if (!Girls[v]) Link(u,v);
else if (order[v][u] < order[v][Girls[v]]) Link(u,v);
else q.push(u);
}
while (!q.empty()) q.pop();
for (int i=; i<=n; ++i)
printf("%d\n",Boys[i]);
}
int main() {
int Case = read();
while (Case--) {
solve();
if(Case) puts("");
}
return ;
}
UVA 1175 - Ladies' Choice的更多相关文章
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...
- 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
- Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...
- UVALive 3989 Ladies' Choice
Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
- UVALive-3989 Ladies' Choice (稳定婚姻问题)
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
- UVALive3989 Ladies' Choice —— 稳定婚姻问题 Gale - Shapely算法
题目链接:https://vjudge.net/problem/UVALive-3989 题解: 题意:有n个男生和n个女生.每个女生对男神都有个好感度排行,同时每个男生对每个女生也有一个好感度排行. ...
- LA 3989 - Ladies' Choice 稳定婚姻问题
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
随机推荐
- python接口测试-项目实践(七)脚本优化
七 脚本优化:重复代码的提取成函数:与项目接口相关的都封装到一个类中:添加手工验证脚本,增加输入值的判断逻辑 将所有与该项目接口相关的封装成类 class ProjectApi: #3个数据源接口 d ...
- 写一个简单的shellcode
0x00 前言 漏洞利用中必不可缺的部分就是shellcode,不会编写shellcode和咸鱼有什么区别,跳出咸鱼第一步. 0x01 系统调用 通过系统调用execve函数返回shell C语言实现 ...
- 【luogu P2065 [TJOI2011]卡片】 假题解
题目链接:https://www.luogu.org/problemnew/show/P2065 辣鸡匈牙利,没有优化贼鸡儿慢 // luogu-judger-enable-o2 #include & ...
- webapi是如何绑定参数的(How WebAPI does Parameter Binding)
原文地址 由于工作原因,要使用ASP.NET WEBAPI(非mvc webapi),前几天时间一直很紧张,所以webapi一直将就用,今天下午好不容易有时间终于看了下,解决了自己一直疑惑的问题,在此 ...
- Redis的安装+哨兵模式+集群
Redis安装(基于2.8版本) 哨兵(Sentinel集群)模式: 集群(基于3.0+)
- data-ng-model 指令
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 任务学习-ucos
1.任务(task)也称作一个线程: 2.一个任务有5种状态:休眠,就绪,运行,挂起,被中断 休眠:任务驻留在程序空间中,还没有交给ucos管理,把任务交给ucos 是通过调用OSTaskCreate ...
- ATK 设计框架辅助工具-代码生成器
在 ATK框架代码中的示例,是用代码生成器生成的. 示例中有三个项目DemoTools.BLL 业务层,DemoTools.UIServer 前端服务层,DemoTools.WebUI 前端是ASP. ...
- CF605A Sorting Railway Cars(递推)
题目描述 An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers ...
- BZOJ3668: [Noi2014]起床困难综合症(贪心 二进制)
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2708 Solved: 1576[Submit][Status][Discuss] Descript ...