HDU 1522 Marriage is Stable 稳定婚姻问题 (再水一发)

http://acm.hdu.edu.cn/showproblem.php?pid=1522\

/*********************************************
    Problem : HDU 1522
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++) //遍历
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --) //反向遍历
#define repS(it,p) for(auto it = p.begin() ; it != p.end() ; it ++) //遍历一个STL容器
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next) //遍历u所连接的点
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m;

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};

map<string,int>m1,m2;
int RankA[505][505]; //男士的追求列表;
int RankB[505][505]; //女士的接受列表;
char nameA[505][105];
char nameB[505][105];

void input() {
    m1.clear(); m2.clear();
    char name[105];
    scanf("%s",name); m1[name] = 1; strcpy(nameA[1],name);
    rep(i,1,n) { scanf("%s",name); m2[name] = i; strcpy(nameB[i],name); RankA[1][i] = i;}
    rep(i,2,n) {
        scanf("%s",name); m1[name] = i; strcpy(nameA[i],name);
        rep(j,1,n) {scanf("%s",name); RankA[i][j] = m2[name];}
    }

    //处理女士
    rep(i,1,n) {
        scanf("%s",name);
        int u = m2[name];
        rep(j,1,n) {
            scanf("%s",name);
            RankB[u][m1[name]] = j; //直接根据RankB就可以对追求的男士进行排序
        }
    }
}

int matchA[505];//男士的匹配情况
int matchB[505];//女士的匹配情况
int lick[505];

void solve() {
    cls(matchA,0) ; cls(matchB,0) ; cls(lick,0);
    int flg = 0;
    while(1) {
        int ok = 0;
        rep(i,1,n) {
            if(!matchA[i]) {
                ok = 1;
                int u = ++lick[i];
                if(u > n) { flg = 1; break;}
                int v = RankA[i][u];
                if(!matchB[v]) {
                    matchB[v] = i; matchA[i] = v;
                }
                else {
                    if(RankB[v][i] < RankB[v][matchB[v]]) {
                        matchA[matchB[v]] = 0;
                        matchB[v] = i; matchA[i] = v;
                    }
                }
            }
        }
        if(flg) break;
        if(!ok) break;
    }
    if(flg) puts("Impossible");
    else {
        rep(i,1,n) {
            printf("%s %s\n",nameA[i],nameB[matchA[i]]);
        }
        puts("");
    }
}  

int main(void) {
    //freopen("a.in","r",stdin);
    while(~scanf("%d",&n)) {
        input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值