标签:des style http color get width
经典的稳定婚姻匹配问题
#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,布布扣,bubuko.com
标签:des style http color get width
原文地址:http://blog.csdn.net/ck_boss/article/details/35289437