zoukankan      html  css  js  c++  java
  • uva 1175 la3898 稳定婚姻问题模板

    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 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 the class. Next, there are N lines, each one containing 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 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

     

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    
    #define MAXN 1111
    int pref[MAXN][MAXN], order[MAXN][MAXN], nxt[MAXN];
    int fb[MAXN], fw[MAXN];
    queue<int> q;
    
    void engage(int man, int woman){
        int m=fb[woman];
        if(m){
            fw[m]=0;
            q.push(m);
        }
        fw[man]=woman;
        fb[woman]=man;
    }
    
    int main(){
        freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int T;
        scanf(" %d", &T);
        while(T--){
            int n, i, j;
            scanf(" %d", &n);
            for(i=1; i<=n; i++){
                for(j=1; j<=n; j++)
                    scanf(" %d", &pref[i][j]);
                nxt[i]=1;
                fw[i]=0;
                q.push(i);
            }
    
            for(i=1; i<=n; i++){
                for(j=1; j<=n; j++){
                    int x;
                    scanf(" %d", &x);
                    order[i][x]=j;
                }
            //    fb[i]=0;
            }
    
            while(!q.empty()){
                int man=q.front();  q.pop();
                int woman=pref[man][nxt[man]++];
                if(!fb[woman]) engage(man, woman);
                else if(order[woman][man]<order[woman][fb[woman]]) engage(man, woman);
                else q.push(man);
            }
            while(!q.empty()) q.pop();
    
            for(i=1; i<=n; i++) printf("%d
    ", fw[i]);
            if(T) puts("");
        }
        return 0;
    }
    
  • 相关阅读:
    去掉百度地图左下角文字和图标。
    使用CMake,且在GCC编译时指定相对源代码路径选项BUG的问题
    一键杀死某些指定进程的脚本
    KMS使用CLion作为IDE来调试
    ubuntu 18.04下安装编译的KMS,依赖库
    ubuntu 18.04下编译最新版本的KMS
    configure.ac中AC_CHECK_LIB的问题
    C/C++下__FILE__参数过长的问题解决办法
    Linux 下 UltraEdit 版本 破解 30 天试用限制
    ubuntu下配置ProFtpd服务使用sqlite3作为后端用户认证
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3320660.html
Copyright © 2011-2022 走看看