当前位置:网站首页>HDU 1914 the stable marriage problem
HDU 1914 the stable marriage problem
2022-07-28 04:55:00 【gongyuandaye】
The question : Stable marriage matching problem , Board question .
Answer key : Stable marriage matching
Pay attention to output in dictionary order , And the output format , The last example does not output blank lines .
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int maxn = 33;
int t, n;
int boy_love_who[maxn][maxn], girl_love_fir[maxn][maxn];
int boy[maxn], girl[maxn], kth[maxn];
char str[maxn << 1];
struct node {
int a, b;
bool operator<(const node x)const {
return a < x.a;
}
}ans[maxn];
inline void G_S() {
for (int i = 1; i <= n; i++) boy[i] = girl[i] = kth[i] = 0;
bool flag, no = 0;
while (true) {
flag = false;
for (int i = 1; i <= n; i++) {
if (!boy[i]) {
int g = boy_love_who[i][++kth[i]];
if (kth[i] > n) {
no = 1;
break;
}
if (!girl[g]) {
girl[g] = i;
boy[i] = g;
continue;
}
else if (girl_love_fir[g][i] < girl_love_fir[g][girl[g]]) {
boy[girl[g]] = 0;
girl[g] = i;
boy[i] = g;
}
flag = true;
}
}
if (no) break;
if (!flag) break;
}
if (no) puts("Impossible");
else {
for (int i = 1; i <= n; i++) ans[i] = {
str[girl[i]], str[i + n] };
sort(ans + 1, ans + n + 1);
for (int i = 1; i <= n; i++) printf("%c %c\n", ans[i].a, ans[i].b);
puts("");
}
}
char s[33];
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
map<char, int> m1, m2;
for (int i = 1; i <= n; i++) {
scanf("%s", s);
m1[s[0]] = i;
str[i] = s[0];
}
for (int i = 1; i <= n; i++) {
scanf("%s", s);
m2[s[0]] = i;
str[i + n] = s[0];
}
for (int i = 1; i <= n; i++) {
scanf("%s", s);
int x = m1[s[0]];
for (int j = 2; j <= n + 1; j++) {
int y = m2[s[j]];
boy_love_who[i][j - 1] = y;
}
}
for (int i = 1; i <= n; i++) {
scanf("%s", s);
int x = m2[s[0]];
for (int j = 2; j <= n + 1; j++) {
int y = m1[s[j]];
girl_love_fir[i][y] = j - 1;
}
}
G_S();
}
return 0;
}
边栏推荐
- Introduction to this pointer
- Special topic of APP performance design and Optimization - poor implementation affecting performance
- What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?
- Dynamic SQL and paging
- [Sylar] framework -chapter20- daemon module
- Printf() print char* str
- Interview fraud: there are companies that make money from interviews
- Internet of things industrial serial port to WiFi module wireless routing WiFi module selection
- Real intelligence has been certified by two of the world's top market research institutions and has entered the global camp of excellence
- C语言ATM自动取款机系统项目的设计与开发
猜你喜欢
![[daily one] visual studio2015 installation in ancient times](/img/b1/066ed0b9e93b8f378c89ee974163e5.png)
[daily one] visual studio2015 installation in ancient times

Histogram of pyplot module of Matplotlib (hist(): basic parameter, return value)

你必需要了解的saas架构设计?

After a year of unemployment, I learned to do cross-border e-commerce and earned 520000. Only then did I know that going to work really delayed making money!

100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel

RT based_ Distributed wireless temperature monitoring system based on thread

Improving the readability of UI layer test with puppeter

猿辅导技术进化论:助力教与学 构想未来学校

What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?

Design and development of C language ATM system project
随机推荐
Mac installs mysql5.7 through brew
(2.4) [service Trojan -slimftp] introduction and use
Service object creation and use
100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel
(克隆虚拟机步骤)
Comprehensively analyze the differences between steam and maker Education
could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node
Evolution of ape counseling technology: helping teaching and learning conceive future schools
Inspire domestic students to learn robot programming education for children
Have you learned the common SQL interview questions on the short video platform?
[Sylar] framework -chapter15 stream module
Can plastics comply with gb/t 2408 - Determination of flammability
Odoo action analysis (action.client, action.act_window, action.server)
你必需要了解的saas架构设计?
The first artificial intelligence security competition starts. Three competition questions are waiting for you to fight
[daily question 1] 735. Planetary collision
Interview fraud: there are companies that make money from interviews
[Sylar] framework Chapter 22 auxiliary module
FreeRTOS learning (I)
塑料可以执行GB/T 2408 -燃烧性能的测定吗