稳定婚姻问题,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. ASP.Net MVC中JSON处理。

    实体数据Model [Serializable] public class UserModel { //public UserModel(string name, string classname, ...

  2. th:each

    <tr th:each="user,userStat:${users}">userStat是状态变量,有 index,count,size,current,even,o ...

  3. [OC] UITabBarController

    1. New CocoaTouch class -> Select Objective C -> named RootViewController 2. Disable APC error ...

  4. asp.net runat="server" && hiddenfield

    runat="server", c#可以直接获得client控件,并且赋值 hiddenfield 可以作为传值,或者界面存值,后台每次读取,并且再赋值到前台,这样前台就可以把上一 ...

  5. 长安铃木经销商爬取(解析xml、post提交、python中使用js代码)

    1.通过火狐浏览器,查找大长安铃木官网中关于经销商的信息主要在两个网页中 http://www.changansuzuki.com/khfw/xml/pro.xml  地域信息 http://www. ...

  6. web.xml常用元素

    web.xml文件是用来初始化配置信息:比如welcome页面.servlet.servlet-mapping.filter.listener.启动加载级别等.当你的web工程没用到这些时,你可以不用 ...

  7. Nested Loop,Sort Merge Join,Hash Join

    三种连接工作方式比较: Nested loops 工作方式是从一张表中读取数据,访问另一张表(通常是索引)来做匹配,nested loops适用的场合是当一个关联表比较小的时候,效率会更高. Merg ...

  8. MVC使用的MetaModel代码生成器模板

    代码生成器能使从一些重复的工作中缓解下来 在最近开发MVC项目中使用到了MetaModel用来设定Model的显示名称,数据限制的代码生成模板,自己第一做代码生成模板还有很多缺陷. 下面是模板代码: ...

  9. Unity3d + UGUI 的多分辨率适配

    原文地址:http://blog.csdn.net/dingkun520wy/article/details/49471789 1.Canvas的属性配置 2.Canvas Scaler的属性配置 3 ...

  10. 搭建Git Server

    windows上如何搭建Git Server   Git在版本控制方面,相比与SVN有更多的灵活性,对于开源的项目,我们可以托管到Github上面,非常方便,但是闭源的项目就会收取昂贵的费用.那么私有 ...