UVALive 3989 Ladies' Choice
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVALive 3989 Ladies' Choice相关的知识,希望对你有一定的参考价值。
经典的稳定婚姻匹配问题
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int maxn=1100; int n; int perfect_boy[maxn][maxn]; int perfect_girl[maxn][maxn]; int future_husband[maxn],future_wife[maxn]; int next[maxn]; queue<int> q; void engage(int boy,int girl) { int m=future_husband[girl]; if(m) { future_wife[m]=0; q.push(m); } future_husband[girl]=boy; future_wife[boy]=girl; } bool lover(int m1,int m2,int girl) { for(int i=1;i<=n;i++) { if(perfect_boy[girl][i]==m1) return true; if(perfect_boy[girl][i]==m2) return false; } } int main() { int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d",&n); memset(perfect_boy,0,sizeof(perfect_boy)); memset(perfect_girl,0,sizeof(perfect_girl)); memset(future_husband,0,sizeof(future_husband)); memset(future_wife,0,sizeof(future_wife)); memset(next,0,sizeof(next)); while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&perfect_girl[i][j]); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) scanf("%d",&perfect_boy[i][j]); q.push(i); } while(!q.empty()) { int boy=q.front(); q.pop(); int girl=perfect_girl[boy][++next[boy]]; if(future_husband[girl]==0) engage(boy,girl); else { int m=future_husband[girl]; if(lover(boy,m,girl)) engage(boy,girl); else q.push(boy); } } for(int i=1;i<=n;i++) printf("%d\n",future_wife[i]); if(T_T) putchar(10); } return 0; }
以上是关于UVALive 3989 Ladies' Choice的主要内容,如果未能解决你的问题,请参考以下文章
SPOJ-EIGHTS Triple Fat Ladies数学规律
UVA1264 && UVAlive4847 Binary Search Tree