题意:稳定婚姻匹配问题,板子题。

题解:稳定婚姻匹配
注意按字典序输出,还有输出格式,最后个样例不输出空行。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int maxn = 33;
int t, n;int boy_love_who[maxn][maxn], girl_love_fir[maxn][maxn];
int boy[maxn], girl[maxn], kth[maxn];
char str[maxn << 1];
struct node {int a, b;bool operator<(const node x)const {return a < x.a;}
}ans[maxn];
inline void G_S() {for (int i = 1; i <= n; i++) boy[i] = girl[i] = kth[i] = 0;bool flag, no = 0;while (true) {flag = false;for (int i = 1; i <= n; i++) {if (!boy[i]) {int g = boy_love_who[i][++kth[i]];if (kth[i] > n) {no = 1;break;}if (!girl[g]) {girl[g] = i;boy[i] = g;continue;}else if (girl_love_fir[g][i] < girl_love_fir[g][girl[g]]) {boy[girl[g]] = 0;girl[g] = i;boy[i] = g;}flag = true;}}if (no) break;if (!flag) break;}if (no) puts("Impossible");else {for (int i = 1; i <= n; i++) ans[i] = { str[girl[i]], str[i + n] };sort(ans + 1, ans + n + 1);for (int i = 1; i <= n; i++) printf("%c %c\n", ans[i].a, ans[i].b);puts("");}
}
char s[33];
int main() {scanf("%d", &t);while (t--) {scanf("%d", &n);map<char, int> m1, m2;for (int i = 1; i <= n; i++) {scanf("%s", s);m1[s[0]] = i;str[i] = s[0];}for (int i = 1; i <= n; i++) {scanf("%s", s);m2[s[0]] = i;str[i + n] = s[0];}for (int i = 1; i <= n; i++) {scanf("%s", s);int x = m1[s[0]];for (int j = 2; j <= n + 1; j++) {int y = m2[s[j]];boy_love_who[i][j - 1] = y;}}for (int i = 1; i <= n; i++) {scanf("%s", s);int x = m2[s[0]];for (int j = 2; j <= n + 1; j++) {int y = m1[s[j]];girl_love_fir[i][y] = j - 1;}}G_S();}return 0;
}

HDU 1914 The Stable Marriage Problem (稳定婚姻匹配)相关推荐

  1. HDOJ 1914 The Stable Marriage Problem

    rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 6553 ...

  2. HDU 1435 Stable Match (稳定婚姻匹配)

    题意:建立一个匹配表,使得一个发射点对应一个接收点,对于某一个发射点来说,它的接收点离它越近那么就会更稳定,同样对于接收点也是一样的情况. 匹配的目标是使得整个网络变得稳定. 对于某2个匹配,比如,( ...

  3. 稳定婚姻匹配(Stable Match, HDOJ 1435, HDOJ 1914, 详解)

    稳定婚姻匹配 HDOJ 1435 Stable Match(此例基于发射点优先的匹配,但题目并未明确表明) Time Limit: 2000/1000 MS (Java/Others) Memory ...

  4. HDU 1522 Marriage is Stable (稳定婚姻匹配)

    题意:稳定婚姻匹配问题,板子题. 题解:稳定婚姻匹配 还好做了这题,之前的板子有点问题,1435数据太弱了. #define _CRT_SECURE_NO_WARNINGS #include<i ...

  5. Marriage is Stable 【HDU - 1522】【稳定婚姻匹配问题】

    题目链接 什么是婚姻匹配问题呢?我做了这道题后就有了这样的体会 -- 比谁更渣??? 我们知道,有N个男生还有N个女生,要把每个男生和女生匹配起来,让他们结婚,但是呢,有这样的情况,就是假如我们现在B ...

  6. The Stable Marriage Problem(POJ-3487)

    Problem Description The stable marriage problem consists of matching members of two different sets a ...

  7. SGU 264 Travel(稳定婚姻匹配)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=264 题意:稳定婚姻匹配. 思路:对于每个男的依次从自己最喜欢的女的开始求婚若女的还 ...

  8. Hdu1914稳定婚姻匹配

    题意: 给出 n 个男生,n 个女生,给出他们对异性的喜欢顺序,求一个稳定的婚姻组合. 思路: 稳定婚姻匹配裸题 #include <cstdio> #include <cstrin ...

  9. ALG1:GS算法,稳定婚姻匹配问题

    ALG1:GS算法,稳定婚姻匹配问题 问题描述如下: (详见NKU计算机上机课程辅助评测) 稳定婚姻匹配 实现代码: (此代码逻辑可通过全部测试样例) 由于代码有一段时间了,一下的展示代码是没有自己的 ...

最新文章

  1. 关于共享单车的供电问题如何解决?
  2. Dynamics 365-关于Solution的那些事(一)
  3. RSEM-Ebseq-差异表达分析-无参
  4. 【PP操作手册】工作中心的维护
  5. JavaWeb-综合案例(用户信息)-学习笔记01【列表查询】
  6. 高级会计师计算机考试中级,会计师需要计算机等级考试吗
  7. 【DP】Bovine Genetics G(P7152)
  8. Linux du查看磁盘文件夹占用容量
  9. activiti页面展示流程图乱码_activiti 5.17 流程图中文乱码问题
  10. 经典sql语句50题_SQL面试经典50题:带你从建表开始
  11. 网页中竖的目录怎么改成横的_一点基础也没有,想快速练漂亮硬笔字,先练什么?怎么练进步明显?...
  12. [Http权威指南]1.Http概况
  13. 【超详细】初中高级软件测试工程师 都需要掌握哪些测试技能
  14. MStar点屏(LVDS接口屏)
  15. 进安全模式提示”Press ENTER to continue loading SPTD.sys”
  16. 戴尔笔记本耳机插着没反应?
  17. 关于MacOS降系统版本的处理方法
  18. [BUU刷题记录]day01-起步
  19. Meanshift 和 Camshift
  20. android手表密码忘了咋办,忘记解锁密码?教你如何重置Android手机的解锁密码

热门文章

  1. 过宇宙元年,ICT技术向产业扎根
  2. Alienware 17 R4安装windows10与ubuntu16.04双系统
  3. arcgis fishnet 单位_【转】ArcGIS生成规则网格(Fishnet)
  4. 推荐一些我觉得非常实用的工具、网站、资源等——持续更新中
  5. python多线程库_python多线程库
  6. java excel 多sheet页_java导入excel操作多sheet页上传
  7. leetcode系列-59. 螺旋矩阵 II
  8. trac 推广 ppt 分享
  9. 计算机专业考cfa,哪些专业适合考CFA?这个专业考CFA,难度直降60%
  10. 了解途家-途家简介-让不动产增值,与家一起旅行!_途家网