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


    题目大意:稳定婚姻问题。。。。

    题目分析:模板题。


    代码如下:

    # include<iostream>
    # include<cstdio>
    # include<queue>
    # include<cstring>
    # include<algorithm>
    using namespace std;
    # define LL long long
    # define REP(i,s,n) for(int i=s;i<n;++i)
    # define CL(a,b) memset(a,b,sizeof(a))
    # define CLL(a,b,n) fill(a,a+n,b)
    
    const int N=1005;
    int fu_husband[N],fu_wife[N],order[N][N],pref[N][N],nxt[N],n;
    queue<int>q;
    
    void engage(int man,int woman)
    {
        int k=fu_husband[woman];
        if(k){
            fu_wife[k]=0;
            q.push(k);
        }
        fu_wife[man]=woman;
        fu_husband[woman]=man;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            while(!q.empty()) q.pop();
            REP(i,1,n+1){
                REP(j,1,n+1) scanf("%d",&pref[i][j]);
                nxt[i]=1;
                fu_wife[i]=0;
                q.push(i);
            }
            REP(i,1,n+1){
                REP(j,1,n+1){
                    int x;
                    scanf("%d",&x);
                    order[i][x]=j;
                }
                fu_husband[i]=0;
            }
            while(!q.empty()){
                int man=q.front();
                q.pop();
                int woman=pref[man][nxt[man]++];
                if(!fu_husband[woman])
                    engage(man,woman);
                else if(order[woman][man]<order[woman][fu_husband[woman]])
                    engage(man,woman);
                else
                    q.push(man);
            }
            REP(i,1,n+1) printf("%d
    ",fu_wife[i]);
            if(T) printf("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    CF1515G
    杂题
    ARC120E
    CF1528F
    ICPC2021银川C
    gym102129F
    杂记6.15
    杂记5.12
    杂记4.1
    杂记3.17
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4940146.html
Copyright © 2020-2023  润新知