UVA 1175 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 an integer N, not greater than $, indicating the number of couples in t He 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 of the 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 of 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
The main topic: There are N boys and n girls in a big school dance, each has a sort of each opposite sex, representing the degree of their liking. Your task is to match boys and girls one by one, so that boys you and girls v do not have the following conditions: 1) boy u and Girl V is not a partner. 2) They like each other more than they like their partner, and if there is a 2, they may leave their partner and make a group. Your task is to find out what his favorite is for every girl, among all the boys who might be dancing with him. Problem-solving ideas: classic stable marital problems. Use the proposal rejection algorithm (Propose-and-reject algorithm).
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;Const intN =1005;typedef Long LongllintNintMen[n][n], Getwifi[n], next[n];intWomen[n][n], gethus[n]; Queue<int>Q;voidInput () {scanf("%d", &n); for(inti =1; I <= N; i++) { for(intj =1; J <= N; J + +) {scanf("%d", &men[i][j]);//Number I for boys J like Girls} Next[i] =1;//The boy who is numbered I, the next object to be invited to danceGetwifi[i] =0;//Number of boys with numbers IQ.push (i); }intX for(inti =1; I <= N; i++) { for(intj =1; J <= N; J + +) {scanf("%d", &x); Women[i][x] = j;//Number I in the minds of girls, numbered x of the boys ranked} Gethus[i] =0;//girl's partner number for I}}voidEngageintManintWoman) {if(Gethus[woman]) {//If the current girl has a partner, abandon him for a more handsome new partner .Getwifi[gethus[woman]] =0;//Abandoned boys back to their single statusQ.push (Gethus[woman]); } Gethus[woman] = man;//New pairGetwifi[man] = woman;}voidSolve () { while(! Q.empty ()) {intman = Q.front (); Q.pop ();intWoman = men[man][next[man]++];if(!gethus[woman]) {//If the current girl is single, then direct pairingEngage (man, woman); }Else if(Women[woman][man] < Women[woman][gethus[woman]) {//If the current male ranking is higher than her partner's in the girl's mind, abandon the current partner and match the new boyEngage (man, woman); }ElseQ.push (man);//Tragic boys no one wants, melted down re-created} for(inti =1; I <= N; i++) {//Output printf("%d\n", Getwifi[i]); }}intMain () {intTscanf("%d", &t); while(t--) { while(! Q.empty ()) Q.pop (); Input (); Solve ();ifTputs(""); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint.
UVA 1175 Ladies ' Choice (Stable marital problems)