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的更多相关文章

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

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

  2. UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)

    题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...

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

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

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

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

  5. UVALive 3989 Ladies' Choice

    Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...

  6. UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)

    题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...

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

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

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

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

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

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

随机推荐

  1. JQUERY实现点击INPUT使光标移动到最后或指定位置

    下面本文章给大家简单介绍一下JQUERY实现点击INPUT使光标移动到最后或指定位置例子,希望对各位有帮助,你要知道面对一个 处女座的 需求者, focus()是远远不够的,比如说“我点进去的时候光标 ...

  2. Linux获取系统当前时间(精确到毫秒)

    #include <stdio.h> #include <time.h> #include <sys/time.h> void sysLocalTime() { t ...

  3. C语言 宏的定义

    #include <stdio.h> // NUM叫做宏名 // 6是用来替换宏名的字符串 #define NUM 6 #define mul(a, b) ((a)*(b)) void t ...

  4. IOS GCDAsyncSocket

    // // ViewController.m // 05.聊天室 // // Created by apple on 14/12/5. // Copyright (c) 2014年 heima. Al ...

  5. 初学Pollard Rho算法

    前言 \(Pollard\ Rho\)是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:\(MillerRabin\)素数测试(关于\(MillerRabin\),可以参考这篇博客:初学Mi ...

  6. ACM-ICPC(10/23)

    贪心 区间相关问题 选择不相交区间: hdu 2037 给定一些区间,选择尽量多的区间,他们互相不交叉.(活动安排问题) 分析:贪心思路是解决活动安排问题的好方案. 按照区间右端点排序,从前往后遍历, ...

  7. Ajax综合应用大全(全面解析)

      AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScrip ...

  8. idea 一次性自动导包

    当复制粘贴一段纯文本代码时,许多类需要导包. 如图 使用optimize imports了,发还是没有办法导入未导入的包,只能删除未使用导包,这跟eclipse不一样,让人很不习惯,查了好多资料都没有 ...

  9. 【luogu P2731 骑马修栅栏】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2731 这个题是欧拉回路的模板题,那么在这里给出一个hierholzer的做法. 对于求欧拉回路的问题,有Fl ...

  10. 使用百度新闻RSS

    function getbaidu() { $result=""; //RSS源地址列表数组 $rssfeed = array("http://news.baidu.co ...