The Stable Marriage Problem
Posted bxd123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Stable Marriage Problem相关的知识,希望对你有一定的参考价值。
和上一题基本一样的
#include <iostream> #include <cstring> #include <map> #define MAXN 30 using namespace std; int mm[MAXN][MAXN], gg[MAXN][MAXN], p[MAXN]; int gg_match[MAXN], mm_match[MAXN]; char idx[2*MAXN]; map<char, int> mp; int main() { int t, n; cin >> t; while (t--) { cin >> n; char ch; int cnt = 0; mp.clear(); for (int i = 0; i < 2 * n; i++) cin >> ch; for (int i = 1; i <= n; i++) { cin >> idx[i] >> ch; mp[idx[i]] = i; for (int j = 1; j <= n; j++) { cin >> ch; if (mp[ch] == 0) { mp[ch] = ++cnt; idx[cnt+n] = ch; } gg[i][j] = mp[ch]; } } for (int i = 1; i <= n; i++) { char gir; cin >> gir >> ch; int a = mp[gir]; for (int j = 1; j <= n; j++) { cin >> ch; int b = mp[ch]; mm[a][b] = j; } } bool flag = true; for (int i = 1; i <= n; i++) p[i] = 1; memset(mm_match, -1, sizeof(mm_match)); memset(gg_match, -1, sizeof(gg_match)); while (flag) { flag = false; for (int i = 1; i <= n; i++) { if (gg_match[i] == -1 && p[i] <= n) { int a = gg[i][p[i]++]; if (mm_match[a] == -1) mm_match[a] = i, gg_match[i] = a; else if (mm[a][i] < mm[a][mm_match[a]]) { gg_match[i] = a; gg_match[mm_match[a]] = -1; mm_match[a] = i; } flag = true; } } } for (int i = 0; i < 26; i++) { for (int j = 1; j <= n; j++) if (idx[j] == ‘a‘ + i) { cout << idx[j] << ‘ ‘ << idx[gg_match[j]+n] << endl; break; } } if (t) cout << endl; } return 0; }
以上是关于The Stable Marriage Problem的主要内容,如果未能解决你的问题,请参考以下文章
稳定婚姻问题(Stable Marriage Problem)
HDU 1522 Marriage is Stable 稳定婚姻匹配(模板题)
Marriage is Stable HDU1522 稳定婚姻问题