※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 3761번 문제인 The Stable Marriage Problem이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/3761
3761번: The Stable Marriage Problem
The first line gives you the number of tests. The first line of each test case contains integer n (0 < n < 27). Next line describes n male and n female names. Male name is a lowercase letter, female name is an upper-case letter. Then go n lines, that descr
www.acmicpc.net
주어진 문제는 Stable Marriage Problem(Stable Matching Problem; 이하 SMP)과 같다. SMP를 O(N^2)에 해결하는 알고리즘으로 Gale-Shapley 알고리즘이 잘 알려져있다.
Stable Marriage Problem의 해는 유일하지 않으며, 다양한 답이 존재할 수 있다. 이 문제에서는 그 답중 Male-optimal한 답을 출력할 것을 요구하고 있다.
남자들이 자신의 선호도를 기준으로 가장 선호하는 여자서부터 먼저 고백을 시도하는 방식의 Gale-Shapley 알고리즘은 항상 Male-Optimal한 답을 보여준다. 아래는 그 간단한 설명이다.
A를 Gale-Shapley로 얻은 매칭, B를 한 남자 m0가 A에서보다 더 높은 우선순위에 있는 여자 f0와 이어진 매칭이라 하자. 이때, A에서 f0와 원래 이어져있었던 남자 m1은 B에서는 f0보다 더 낮은 우선순위에 있는 여자와 이어지면 unstable해지므로, f0보다 더 높은 순위에 있는 다른 여자 f1과 이어져야한다. 따라서 m1은 B에서는 A에서보다 더 높은 우선순위에 있는 여자 f1과 이어져있다.
이 문제에서 남자의 수와 여자의 수는 유한하므로, 위와 같은 논리를 끝없이 전개해나갈 수 없다. (즉, 모순이다.) 따라서 m0와 같은 남자가 존재하는 stable matching은 존재하지 않는다.
입력 형식에 유의하여 관계를 읽고 문제를 해결하자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int ctoi[128];
char itoM[100], itoF[100];
vector<char> M, F;
int MtoF[1001][1001];
int FtoM[1001][1001];
int iter[1001];
int matching[1001]; // F가 matching된 사람
int ans[1001];
void solve() {
M.clear(), F.clear();
memset(iter, 0, sizeof(iter));
memset(matching, 0, sizeof(matching));
int N; cin >> N;
for (int i = 0; i < N * 2; i++) {
char c; cin >> c;
if ('a' <= c && c <= 'z') M.emplace_back(c);
else F.emplace_back(c);
}
sort(M.begin(), M.end());
sort(F.begin(), F.end());
for (int i = 0; i < N; i++) {
ctoi[M[i]] = i + 1;
itoM[i + 1] = M[i];
ctoi[F[i]] = i + 1;
itoF[i + 1] = F[i];
}
for (int i = 1; i <= N; i++) {
string s; cin >> s;
int ii = ctoi[s[0]];
for (int j = 2; j < N + 2; j++) {
int x = ctoi[s[j]];
MtoF[ii][j - 1] = x;
}
}
for (int i = 1; i <= N; i++) {
string s; cin >> s;
int ii = ctoi[s[0]];
for (int j = 2; j < N + 2; j++) {
int x = ctoi[s[j]];
FtoM[ii][x] = j - 1;
}
}
queue<int> que;
for (int i = 1; i <= N; i++) {
que.push(i);
}
while (!que.empty()) {
int curM = que.front(); que.pop();
int curF = MtoF[curM][++iter[curM]];
int& matchingF = matching[curF];
if (matchingF == 0) matchingF = curM;
else if (FtoM[curF][curM] < FtoM[curF][matchingF]) {
que.push(matchingF);
matchingF = curM;
}
else que.push(curM);
}
for (int i = 1; i <= N; i++) ans[matching[i]] = i;
for (int i = 1; i <= N; i++) cout << itoM[i] << ' ' << itoF[ans[i]] << '\n';
cout << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T; cin >> T;
while (T--) {
solve();
}
}
'BOJ' 카테고리의 다른 글
[BOJ 1623 // C++] 신년 파티 (0) | 2022.05.25 |
---|---|
[BOJ 21219 // C++] Restaurants (0) | 2022.05.24 |
[BOJ 12718 // C++] Crop Triangles (Large) (0) | 2022.05.22 |
[BOJ 6135 // C++] Cow Hurdles (0) | 2022.05.22 |
[BOJ 12719 // C++] Number Sets (Small) (0) | 2022.05.22 |