POJ3487 The Stable Marriage Problem
来源:互联网 发布:mac mysql dmg 安装 编辑:程序博客网 时间:2024/05/01 12:51
题意:稳定婚姻问题。
分析:
od(i, j)表示编号为i的女士心目中,编号为x的男士的排名。
pf(i, j)表示编号为i的男士第j喜欢的人。
f(0, i)表示编号为i的女士的未婚夫,f(1, i)表示编号为i的男士的未婚妻。
nxt(i)表示编号为i的男士下一个求婚对象的排名。
q队列里存着所有未订婚的男士。
这题输入有些奇怪,小心一点就是了。
然后用站在女性角度考虑好像不符合题目要求,得站在男性角度考虑,白白WA一次。
这题代码就没有什么难理解的地方了。
#include <cstdio>#include <queue>#include <algorithm>using namespace std;const int N = 30;char str[N], name[N*2];int T, n, x, od[N][N], pf[N][N], nxt[N], f[2][N];queue<int> q;void date(int x, int y) {int m = f[0][y];if(m) f[1][m] = 0, q.push(m);f[1][x] = y, f[0][y] = x;}int main() {scanf("%d", &T);while(T--) {scanf("%d", &n);for(int i = 1; i <= n*2; i++) scanf("%s", str), name[i] = str[0];sort(name+1, name+n+1);for(int i = 1; i <= n; i++) {scanf("%s", str);for(int j = 2; j < n+2; j++) pf[str[0]-'a'+1][j-1] = str[j]-'A'+1;nxt[str[0]-'a'+1] = 1, f[1][str[0]-'a'+1] = 0;q.push(str[0]-'a'+1);}for(int i = 1; i <= n; i++) {scanf("%s", str);for(int j = 2; j < n+2; j++) od[str[0]-'A'+1][str[j]-'a'+1] = j-1;f[0][str[0]-'A'+1] = 0;}while(!q.empty()) {int u = q.front(); q.pop();int v = pf[u][nxt[u]++];if(!f[0][v]) date(u, v);else if(od[v][u] < od[v][f[0][v]]) date(u, v);else q.push(u);}for(int i = 1; i <= n; i++)printf("%c %c\n", name[i], f[1][name[i]-'a'+1]+'A'-1);if(T) printf("\n");}return 0;}
2 0
- POJ3487 The Stable Marriage Problem
- poj3487-The Stable Marriage Problem
- 【稳定匹配】:poj3487,The Stable Marriage Problem
- poj3487 The Stable Marriage Problem(最稳定的婚姻)
- 【稳定婚姻问题】poj3487 The Stable Marriage Problem
- poj3487 & hdu1914 The Stable Marriage Problem 稳定婚姻系统
- poj3487(Gale-Shapley算法) The Stable Marriage Problem(稳定婚姻问题)
- HDOJ 1914 The Stable Marriage Problem
- POJ 3487 The Stable Marriage Problem
- HDOJ 1914 The Stable Marriage Problem
- HDU 1914 The Stable Marriage Problem
- Stable Marriage Problem
- stable marriage problem
- Stable marriage problem笔记
- POJ 3487 The Stable Marriage Problem (稳定婚姻问题)
- POJ 3487 The Stable Marriage Problem 稳定婚姻问题
- The Stable Marriage Problem 稳定婚姻问题与应用
- 稳定婚姻匹配问题 EOJ 162 The Stable Marriage Problem
- 我的Android进阶之旅------>解决Jackson等第三方转换Json的开发包在开启混淆后转换的实体类数据都是null的bug
- map映照容器
- 用PHP写自定义的简单WEB服务器
- Android Intent意图详解
- C++学习笔记之七 复合类型---枚举
- POJ3487 The Stable Marriage Problem
- NYOJ171 聪明的kk
- C语言中int类型数的表示范围及形式
- HDU1010-Tempter of the Bone
- Git学习7:Git中的里程碑
- 【Android动画九章】-AlphaAnimation(渐变动画)
- 测试基于gtid模式主从复制下数据不一致的处理
- IO学习(三)常见IO类继承关系图
- Java并发: CountDownLatch、CyclicBarrier和Semaphore