【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\)的距离 两个 ...
随机推荐
- 关于html的下载功能
新项目基本告一段落,第一次完成前后端分离的集成,遇到的坑自然不少. 来说说第一天遇到的其中一个坑吧. ——关于下载的问题... 以前的做法,大家都喜爱用<a></a>标签吧.而 ...
- butterknife7.0.1使用
1.官网:http://jakewharton.github.io/butterknife/ Introduction Annotate fields with @Bind and a view ID ...
- Ubuntu上部署C# 网站 步骤简单记录
对于刚接触linux的同学,由于命令不熟悉,所以要想在上面部署C#网站,容易迷茫,可以参考此简易步骤: 安装 mono: apt-get install mono 按tab搜索 找到mono相关的组 ...
- EasyUI + EF + MVC4 后台截图
到目前完成的页面截图,完成了增删改查几项功能的技术测试,在解决几个小问题,就重新设计结构开始一个完整的后台开发,坚持用博客和云笔记记录开发过程.
- POJ 2516 最小费用最大流
每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...
- cocos2dx之C++调用Lua
原文地址:http://blog.csdn.net/dingkun520wy/article/details/49839701 1.引入头文件 #include "cocos2d.h&quo ...
- Xcode文档下载与安装路径
https://developer.apple.com/library/downloads/docset-index.dvtdownloadableindex ~/Library/Developer/ ...
- Objective-C中的数据类型、常量、变量、运算符与表达式
1.Objective-C中的数据类型: Objective-C中的基本数据类型有:int.char(-128-127).float.double.BOOL,Byte(0-255) Id类型相当于(等 ...
- filter_map
#!/usr/bin/env python # -*- coding:utf-8 -*- ret = filter( lambda x: x < 3, [1, 2, 3, 4, 5]) prin ...
- Xcode 合并分支报错
原理和操作步骤见如下转载的两篇文章, 我所使用的 svn 客户端软件是 Mac 下面的 Versions.app v1.06 这个版本包含一个多人开发的bug bug 的解决方案见我之前转载的两篇文章 ...