Problem I–ladies ' Choice
Background
Teenagers from the local high school has asked you-to-help them with the organization of next year's Prom. The idea was to find a suitable date for everyone in the class in a fair and civilized. So, they has 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 is equal numbers of boys and girls.
Problem
Given a set of preferences, set up the blind dates such that there is no other than people of opposite sex who would both Rather has each of the other than their the current partners. Since It was decided the Prom is Ladies ' Choice, we want to produce the best possible Choice for the girls.
Input
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, indicating the number of cou Ples in the class. Next, there is N lines, each one containing the "all" the integers from 1 to N, ordered according to the Girl ' s preferences. Next, there is N lines, each one containing all the integers from 1 to N, ordered according to the boy ' S preferences.
Output
The output for each dataset consists of a sequence of N lines, where the i-th line contains the number O f the boy assigned to the i-th Girl (from 1 to N). Print a blank line between datasets.
Sample 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
Sample Output
1
2
5
3
4
Test instructions: Tell n boys and n girls to dance together, they are willing to dance with their favorite people. For the current pairing situation, A and C, B and D, if a likes B more than C, B likes a more than D, then these two pairs will be torn apart, because both A and B are more willing to be with each other. Then, it is the most stable to ask how to pair now. Enter the first n behavior of boys to the girl's liking degree sort, after n behavior girls to the boy's liking degree sort. The output is a female number paired with n males.
Analysis: This problem involves the marriage stability algorithm, this algorithm is also called the proposal-Reject algorithm (Propose-and-reject algorithm). The detailed operation of this algorithm is probably as follows. There are n pairs of men and women, for each round of operation, the men to pursue their own have not pursued the favorite girls, and girls can choose to agree or refuse, even if the girl has a partner, but also can abandon before, and accept the present. When everyone is paired successfully, the algorithm can end. So this question will not appear that someone does not have a partner, the answer is not. If a girl does not have a partner, it shows that no one has pursued her, however, a boy before giving up, is bound to pursue all the girls first. So that everyone can be paired to succeed.
Let's talk about the stability of this pairing scheme. For the current pairing situation, A and C, B and D, if a schoolgirl likes B more than C, while B likes A also more than D, then, b in and D before, must first pursue a this girl. However, every time the girl in exchange for the object, must have met a better. So there is no better case for B than C, and B has been rejected by a before, so D is what he feels best now.
Although the whole process, are girls choose Boys, constantly have boys are rejected. But this game is really good for boys. Because every boy has the greatest possibility to be with his favorite person, unless the other side rejected himself. However, girls are out of a very passive state, because their favorite people, may have never pursued their own. So, perhaps in turn, let all the girls to pursue the choice of boys, then for girls will be more advantageous, they have more opportunities and their favorite people together. It also does not affect the stability of the algorithm.
#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath > #include <queue>using namespace std;queue<int>q;//single man queue int manlike[1009][1009];int womanlike[1009] [1009];int nt[1009];//men next to choose the first favorite person int fwife[1009];int Fhusbond[1009];int N;int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d", &n); while (!q.empty ()) Q.pop (); for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) scanf ("%d", &manlike[i][j]); Q.push (i); Nt[i]=1; fwife[i]=0; } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {int k; scanf ("%d", &k); Ms. Womanlike[i][k]=j;//ranked Fhusbond[i]=0 for men who liked the number k; } while (!q.empty ()) {int Man=q.front (); Q.pop (); int Woman=manlike[man][nt[man]]; nt[man]++; if (fhusbond[woman]==0) {Fhusbond[woman]=man; Fwife[man]=woman; } else if (Womanlike[woman][man] < Womanlike[woman][fhusbond[woman]) {FWIFE[FHUSB ond[woman]]=0; Q.push (Fhusbond[woman]); Fhusbond[woman]=man; Fwife[man]=woman; } else Q.push (man); }//cout<< "*******" <<endl; for (int i=1;i<=n;i++) printf ("%d\n", Fwife[i]); if (t>0) puts (""); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
LA3987 Ladies ' Choice marriage stability algorithm