ZOJ 1576 Marriage is Stable(稳定婚姻问题)
来源:互联网 发布:惠州网络 问政平台 编辑:程序博客网 时间:2024/05/12 21:30
#include<iostream>#include<queue>#include<cmath>#include<map>#include<cstdio>using namespace std;const int maxn=500+5;int pref[maxn][maxn],order[maxn][maxn],Next[maxn];int future_husband[maxn],future_wife[maxn];queue<int> q;map<string,int> Boy,Gril;string boy[maxn],gril[maxn];void engage(int man,int woman){ int m=future_husband[woman]; if(m) { future_wife[m]=0; q.push(m); } future_husband[woman]=man; future_wife[man]=woman;}int main(){ int n; while(~scanf("%d",&n)) { Boy.clear();Gril.clear(); string ss; for(int i=1;i<=n;i++){ cin>>boy[i]; Boy[boy[i]]=i; for(int j=1;j<=n;j++){ string g; cin>>g; if(i==1) { gril[j]=g; Gril[g]=j; pref[i][j]=Gril[g]; } else{ pref[i][j]=Gril[g]; } } Next[i]=1; future_wife[i]=0; q.push(i); } for(int i=1;i<=n;i++){ string g; cin>>g; int idx=Gril[g]; for(int j=1;j<=n;j++){ string b; cin>>b; order[idx][Boy[b]]=j; } future_husband[i]=0; } while(!q.empty()) { int man=q.front();q.pop(); int woman=pref[man][Next[man]++]; if(future_husband[woman]==0) engage(man,woman); else if(order[woman][man]<order[woman][future_husband[woman]]) engage(man,woman); else q.push(man); } while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) { cout<<boy[i]<<" "<<gril[future_wife[i]]<<endl; } } return 0;}
0 0
- ZOJ 1576 Marriage is Stable 稳定婚姻问题
- ZOJ 1576 Marriage is Stable(稳定婚姻问题)
- 【稳定婚姻问题】hdu1522 Marriage is Stable
- hdu 1522Marriage is Stable(稳定婚姻问题)
- HDU 1522 Marriage is Stable 稳定婚姻问题
- hdu 1522 Marriage is Stable(稳定婚姻问题)
- 稳定婚姻问题 Poj 3487 The Stable Marriage Problem + Hdu 1522 Marriage is Stable (二分图稳定匹配)
- zoj 1576 Marriage is Stable
- HDU 1522 Marriage is Stable 稳定婚姻匹配
- Marriage is Stable (稳定婚姻匹配GS算法)
- HDU 1522 Marriage is Stable 稳定婚姻问题 (再水一发)
- POJ 3487 The Stable Marriage Problem (稳定婚姻问题)
- POJ 3487 The Stable Marriage Problem 稳定婚姻问题
- The Stable Marriage Problem 稳定婚姻问题与应用
- 稳定婚姻匹配问题 EOJ 162 The Stable Marriage Problem
- The Stable Marriage Problem(稳定婚姻问题)
- poj 3487 The Stable Marriage Problem(稳定婚姻问题)
- poj 3478 The Stable Marriage Problem 稳定婚姻问题
- web QQ协议
- 数据库连接池
- Sed使用的变量里含有转义字符的解决
- 解决Glide加载图片被拉伸
- Oracle 通过pivot和unpivot实现行列转换
- ZOJ 1576 Marriage is Stable(稳定婚姻问题)
- 编程语言中『一等公民』是什么公民?
- angular1x初始与架构演进(二)
- 流水账式java基础Summary
- AngularJS学习之四(分支之二):架构概述
- Linux 学习_在Linux下面安装eclipse
- visual studio配置中包含目录和附加包含目录的区别以及auto-linking
- exercise 20 函数和文件
- androi自定义问题总结