Uvalive 3989 ladies 'choice

Source: Internet
Author: User


Classic stable marriage matching


Uvalive-3989ladies 'choice
Time limit:6000 ms Memory limit:Unknown 64bit Io format:% LLD & % LlU

[Submit] [Go Back] [Status]

Description

Problem I-Ladies? Choice

Background

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.

Problem

Given a set of preferences, set up the blind dates such that there are no other two people of opposite sex who wowould 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

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 1,000, indicating the number of couples in the class. Next, there areNLines, each one containing the all the integers from 1N, Ordered according to the girl? S preferences. Next, there areNLines, each one containing all the integers from 1N, Ordered according to the boy? S preferences.

Output

The output for each dataset consists of a sequenceNLines, whereI-thLine contains the number of the boy assigned toI-thGIRL (from 1N). 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;}





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.