UVA 1175 Ladies' Choice 稳定婚姻问题

题目链接:

题目

Ladies' Choice
Time Limit: 6000MS
Memory Limit: Unknown
64bit IO Format: %lld & %llu

问题描述

Teenagers from the local high school have asked you to help them with the organization of next year’s
Prom. The idea is to find a suitable date for everyone in the class in a fair and civilized way. So,
they have organized a web site where all students, boys and girls, state their preferences among the
class members, by ordering all the possible candidates. Your mission is to keep everyone as happy as
possible. Assume that there are equal numbers of boys and girls.
Given a set of preferences, set up the blind dates such that there are no other two people of opposite
sex who would both rather have each other than their current partners. Since it was decided that the
Prom was Ladies’ Choice, we want to produce the best possible choice for the girls.

输入

Input consists of multiple test cases the first line of the input contains the number of test cases.
There is a blank line before each dataset.
The input for each dataset consists of a positive integer N, not greater than 1,000, indicating the
number of couples in theclass. Next, there are N lines, each onecontaining the all the integers from 1
to N,ordered according to the girl’s preferences. Next, there are N lines, each one containing all the
integers from 1 to N, ordered according to the boy’s preferences.

输出

The output for each dataset consists of a sequence of N lines, where the i-th line containsthe number
of the boy assigned to the i-th girl (from 1 to N).
Print a blank line between datasets.

样例

input
1
5
1 2 3 5 4
5 2 4 3 1
3 5 1 2 4
3 4 2 1 5
4 5 1 2 3
2 5 4 1 3
3 2 4 1 5
1 2 4 3 5
4 1 2 5 3
5 3 2 4 1

output
1
2
5
3
4

题意

男生女生一一配对且男生u和女生v不存在以下情况:

  • 男生u和女生v不是舞伴;
  • 他们喜欢对方的程度都大于各自当前舞伴的程度。

现在要求对每一个女生,在所有可能和她跳舞的男生中,找出她最喜欢的那个。

题解

裸的稳定婚姻问题。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int maxn = 1111;
int pre[maxn][maxn], order[maxn][maxn], ne[maxn];
int future_husband[maxn], future_wife[maxn];
queue<int> Q;
int n;

void engage(int man, int woman) {
	int m = future_husband[woman];
	if (m) {
		future_wife[m] = 0;
		Q.push(m);
	}
	future_wife[man] = woman;
	future_husband[woman] = man;
}

void init() {
	while (!Q.empty()) Q.pop();
	memset(future_husband, 0, sizeof(future_husband));
	memset(future_wife, 0, sizeof(future_wife));
	memset(ne, 0, sizeof(ne));
}

int main() {
	int tc;
	scanf("%d", &tc);
	while (tc--) {
		scanf("%d", &n);
		init();
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				scanf("%d", &pre[i][j]);
			}
			Q.push(i);
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				int x; scanf("%d", &x);
				order[i][x] = j;
			}
		}
		while (!Q.empty()) {
			int man = Q.front(); Q.pop();
			int woman = pre[man][++ne[man]];
			if (!future_husband[woman] || order[woman][man]<order[woman][future_husband[woman]]) {
				engage(man, woman);
			}
			else Q.push(man);
		}
		for (int i = 1; i <= n; i++) printf("%d\n", future_wife[i]);
		if (tc) printf("\n");
	}
	return 0;
}
posted @ 2016-07-04 00:31  fenicnn  阅读(175)  评论(0编辑  收藏  举报