【HDOJ】1914 The Stable Marriage Problem
稳定婚姻问题,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的更多相关文章
- HDOJ 1914 The Stable Marriage Problem
rt 稳定婚姻匹配问题 The Stable Marriage Problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 6553 ...
- 【HDOJ6687】Rikka with Stable Marriage(Trie树,贪心)
题意:给定两个长均为n的序列a和b,要求两两配对,a[i]和b[j]配对的值为a[i]^b[j],求配对后的值之和的最大值 n<=1e5,a[i],b[i]<=1e9 思路:和字典序最大的 ...
- 【HDU6687】Rikka with Stable Marriage(Trie树 贪心)
题目链接 大意 给定\(A,B\)两个数组,让他们进行匹配. 我们称\(A_i\)与\(B_j\)的匹配是稳定的,当且仅当目前所剩元素不存在\(A_x\)或\(B_y\)使得 \(A_i\oplus ...
- 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage Problem Description The stable marriage problem consists of matching members o ...
- 【转】稳定婚姻问题(Stable Marriage Problem)
转自http://www.cnblogs.com/drizzlecrj/archive/2008/09/12/1290176.html 稳定婚姻是组合数学里面的一个问题. 问题大概是这样:有一个社团里 ...
- 【BZOJ2318】Spoj4060 game with probability Problem 概率
[BZOJ2318]Spoj4060 game with probability Problem Description Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬 ...
- The Stable Marriage Problem
经典稳定婚姻问题 “稳定婚姻问题(The Stable Marriage Problem)”大致说的就是100个GG和100个MM按照自己的喜欢程度给所有异性打分排序.每个帅哥都凭自己好恶给每个MM打 ...
- [POJ 3487]The Stable Marriage Problem
Description The stable marriage problem consists of matching members of two different sets according ...
- 【CF954I】Yet Another String Matching Problem(FFT)
[CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...
随机推荐
- WCF 宿主与通信模式(二)
宿主 每个WCF服务都必须托管在Windows进程中,该进程称为宿主进程(host process) 单个宿主进程可以托管多个服务,相同的服务类型也可以托管在多个宿主进程中. wcf中托管服务一般有一 ...
- bower——库管理工具
bower了解: 随着网页功能的复杂化,各种网页效果的实现,现在单一的一个或两个库文件或许已经不能够满足我们的需要,但当有很多的库文件的时候,手动编辑已经不能胜任,对于引入的库文件而言,往往都是牵一发 ...
- PHP服务器负载判断
<?php header("content-type:text/html;charset=utf-8"); // echo PHP_OS;exit; // echo get_ ...
- JavaScript加强之自定义callback示例
callback回调函数在本文以自定义的方式出现. html: <select id="select"> <option value="111& ...
- YII千万级PV架构经验分享--俯瞰篇--架构即产品
hello,大家好,我是方少,今天想问大家一个问题,如果我们自己就是产品,那么我们怎样才能说服别人,我们是最优秀的呢?高学历,不错,别人成功过了.会php,java,c#,python不行再学c++, ...
- HttpUnit学习笔记
<!-- htmlUnit --> <dependency> <groupId>net.sourceforge.htmlunit</groupId> & ...
- 添加打印机的时候怎样说windows没法连接到打印机毛病为0x00000002
把PrinterExtensionsandNotifications这个服务启动1下试试 PrintSpooler服务停止然后再启用试试
- 计算器(delphi)
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- PAT乙级真题1005. 继续(3n+1)猜想 (25)(解题)
原题: 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证 ...
- PHP微信开发代码
1,SAE上申请服务器 2,绑定测试账号 3,Token验证 <?php /* http://www.cnblogs.com/xrhou12326/ CopyRight 2014 All Rig ...