Uvalive 3989 ladies& #39; Choice

Source: Internet
Author: User


The classic stable marriage matching problem


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

[Submit]   [Go back] [Status]

Description

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 integerN, not greater than, indicating the number of coup Les 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

Source

Southwestern 2007-2008

[Submit]   [Go back] [Status]



#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < queue>using namespace Std;const int maxn=1100;int n;int perfect_boy[maxn][maxn];int perfect_girl[maxn][maxn];int Future_husband[maxn],future_wife[maxn];int next[maxn];queue<int> q;void Engage (int boy,int girl) {int m=future_    Husband[girl];        if (m) {future_wife[m]=0;    Q.push (m);    } Future_husband[girl]=boy; Future_wife[boy]=girl;}        BOOL Lover (int m1,int m2,int girl) {for (int i=1;i<=n;i++) {if (PERFECT_BOY[GIRL][I]==M1) return true;    if (PERFECT_BOY[GIRL][I]==M2) return false;    }}int Main () {int t_t;    scanf ("%d", &t_t);        while (t_t--) {scanf ("%d", &n);        memset (perfect_boy,0,sizeof (Perfect_boy));        memset (perfect_girl,0,sizeof (Perfect_girl));        memset (future_husband,0,sizeof (Future_husband));        memset (future_wife,0,sizeof (Future_wife));        memset (Next,0,sizeof (next));while (!q.empty ()) Q.pop ();        for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) scanf ("%d", &perfect_girl[i][j]);            for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) scanf ("%d", &perfect_boy[i][j]);        Q.push (i);            } while (!q.empty ()) {int Boy=q.front (); Q.pop ();            int Girl=perfect_girl[boy][++next[boy]];            if (future_husband[girl]==0) engage (boy,girl);                else {int M=future_husband[girl];                if (Lover (Boy,m,girl)) engage (boy,girl);            else Q.push (boy);        }} for (int i=1;i<=n;i++) printf ("%d\n", Future_wife[i]);    If (T_T) Putchar (10); } return 0;}





Uvalive 3989 ladies& #39; Choice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.