稳定婚姻问题,Gale-Shapley算法可解。

 /* 1914 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
char s[];
char wname[];
int pref[maxn][maxn];
int order[maxn][maxn];
int nxt[maxn];
int future_husband[maxn];
int future_wife[maxn];
queue<int> Q;
int n;
int ID[]; void engage(int man, int woman) {
int m = future_husband[woman]; if (m) {
future_wife[m] = ;
Q.push(m);
} future_husband[woman] = man;
future_wife[man] = woman;
} void solve() {
while (!Q.empty()) {
int man = Q.front();
Q.pop();
int woman = pref[man][nxt[man]++];
if (!future_husband[woman]) {
engage(man, woman);
} else if (order[woman][man] < order[woman][future_husband[woman]]) {
engage(man, woman);
} else {
Q.push(man);
}
} rep(i, 'a', 'z'+) {
if (!ID[i])
continue;
printf("%c %c\n", i, wname[future_wife[ID[i]]]);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t;
int nlc, nuc; scanf("%d", &t);
while (t--) {
scanf("%d", &n);
memset(ID, , sizeof(ID));
nlc = nuc = ;
rep(i, , n+n) {
scanf("%s", s);
if (islower(s[])) {
ID[s[]] = ++nlc;
} else {
ID[s[]] = ++nuc;
wname[nuc] = s[];
}
} while (!Q.empty())
Q.pop(); int lid, uid; rep(i, , n) {
scanf("%s", s);
lid = ID[s[]];
rep(j, , n+) {
uid = ID[s[+j]];
pref[lid][j] = uid;
}
nxt[lid] = ;
future_wife[lid] = ;
Q.push(lid);
} rep(i, , n) {
scanf("%s", s);
uid = ID[s[]];
rep(j, , n+) {
lid = ID[s[+j]];
order[uid][lid] = j;
}
future_husband[uid] = ;
}
solve();
if (t)
putchar('\n');
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】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. 【HDOJ6687】Rikka with Stable Marriage(Trie树,贪心)

    题意:给定两个长均为n的序列a和b,要求两两配对,a[i]和b[j]配对的值为a[i]^b[j],求配对后的值之和的最大值 n<=1e5,a[i],b[i]<=1e9 思路:和字典序最大的 ...

  3. 【HDU6687】Rikka with Stable Marriage(Trie树 贪心)

    题目链接 大意 给定\(A,B\)两个数组,让他们进行匹配. 我们称\(A_i\)与\(B_j\)的匹配是稳定的,当且仅当目前所剩元素不存在\(A_x\)或\(B_y\)使得 \(A_i\oplus ...

  4. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)

    The Stable Marriage Problem   Description The stable marriage problem consists of matching members o ...

  5. 【转】稳定婚姻问题(Stable Marriage Problem)

    转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...

  6. 【BZOJ2318】Spoj4060 game with probability Problem 概率

    [BZOJ2318]Spoj4060 game with probability Problem Description Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬 ...

  7. The Stable Marriage Problem

    经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...

  8. [POJ 3487]The Stable Marriage Problem

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

  9. 【CF954I】Yet Another String Matching Problem(FFT)

    [CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...

随机推荐

  1. JS焦点图,JS 多个页面放多个焦点图

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. phpstorm配置取消掉63342

    http://ask.csdn.net/questions/171665

  3. 用JS写的放大镜

    代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  4. php 批量替换html标签的实例代码

    php批量替换html标签的实例代码分享.   1.把html元素全部去掉,或者保留某几个html标签 <?php $text = '<p>Test paragraph.</p ...

  5. 修改ECSHOP注册只需要email,并且使用email作为账号

    现在很多主流的电商网站都直接用email作为会员账号,好处是省得用户既要想一个用户名,又要填邮箱,邮箱对于电商网站的重要性不用多提,因此用email作为会员的唯一标识是很合理的做法,本文给出针对ECS ...

  6. DOM基础总结

    一.简介 1.什么是DOM 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式 ...

  7. 2015-4-2的阿里巴巴笔试题:乱序的序列保序输出(bit数组实现hash)

    分布式系统中的RPC请求经常出现乱序的情况.写一个算法来将一个乱序的序列保序输出.例如,假设起始序号是1,对于(1, 2, 5, 8, 10, 4, 3, 6, 9, 7)这个序列,输出是:123, ...

  8. Head of a Gang (map+邻接表+DFS)

    One way that the police finds the head of a gang is to check people's phone calls. If there is a pho ...

  9. oracle游标小试

    有时候需要大面积的修改数据,这个时候用循环语句效率不高.而临时表又不能满足点对点修改的时候,游标似一种不错的选择(PS:好像游标也是为循环而生的吧) 现在有两张表 t1(ryid number,nam ...

  10. GridView ItemCommand

    GridView ItemCommand中取某行某列的值方法,这里提供两个常用的: 一.用CommandArgument属性取值页面如下: <asp:TemplateColumn HeaderT ...