UVALive - 3989——Ladies' Choice (稳定婚姻问题)

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
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.
Output
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.
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

题意:给出n个数,代表n个男生和n个女生。n*2行,第一个n行代表每个女生喜欢的男生的优先排序。第一个数就是最喜欢的女生。第二个n行代表男生对女生的排序。要求一一配对,使得男u和女v不存在以下条件:1.u和v不是舞伴。2.他们喜欢对方的程度都大于喜欢各自当前舞伴的程度。

白书思路:所有男生按最喜欢的顺序来一一向女生求伴,女生如果喜欢他超过当前伴侣就换伴侣,或者女生没有伴侣也会选择他。当所有男生都找到伴结束。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
queue<int>q;
int t,n,x,v;
int A[1010][1010],B[1010][1010],X[1010],Y[1010],nextt[1010];
void mam(int x,int y)
{
    if(Y[y])
        q.push(Y[y]);
    Y[y]=x;
    X[x]=y;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&A[i][j]);
            }
            X[i]=0;
            q.push(i);
            nextt[i]=1;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&x);
                B[i][x]=j;
            }
            Y[i]=0;
        }
        while(!q.empty())
        {
            x=q.front();
            q.pop();
            v=A[x][nextt[x]++];
            if(Y[v]==0)
            {
                mam(x,v);
            }
            else if(B[v][Y[v]]>B[v][x])
            {
                mam(x,v);
            }
            else
                q.push(x);
        }
        for(int i=1;i<=n;i++)
        {
            printf("%d\n",X[i]);
        }
        if(t)
            printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/86477927