题目大意
有$N$个男生和$N$个女生,已知每个男生对每个女生的喜欢程度,和每个女生对每个男生的喜欢程度。
找到一种搭配方式使得总的满意程度最高。
题目分析
又一道稳定婚姻问题模板题,与上一题一模一样。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<climits> #include<vector> #include<cstdio> #include<cmath> #include<queue> #include<map> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
map<string,int>M; map<int,string>FM; string a[1005][505]; int n,Next[505],RankMan[505][505],RankWoman[1005][505],Husband[1005],Wife[505];
int main() { ios::sync_with_stdio(false); cin>>n; for(int i=1; i<=2*n; i++) { string Name,Name2; cin>>Name; if(i<=n)Name2=Name+'('; else Name2=Name+')'; M[Name2]=i; FM[i]=Name; for(int j=1; j<=n; j++) { string tmp; cin>>tmp; if(i<=n)tmp+=')'; else tmp+='('; a[i][j]=tmp; } } queue<int>Q; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++)RankMan[i][j]=M[a[i][j]]; Q.push(i); Next[i]=1; } for(int i=n+1; i<=2*n; i++) for(int j=1; j<=n; j++) RankWoman[i][M[a[i][j]]]=j; while(!Q.empty()) { int Now=Q.front(); Q.pop(); int Object=RankMan[Now][Next[Now]++]; if(!Husband[Object]) { Husband[Object]=Now; Wife[Now]=Object; } else { int Enemy=Husband[Object]; if(RankWoman[Object][Enemy]>RankWoman[Object][Now]) { Husband[Object]=Now; Wife[Now]=Object; Wife[Enemy]=0; Q.push(Enemy); } else Q.push(Now); } } for(int i=1; i<=n; i++)cout<<FM[i]<<" "<<FM[Wife[i]]<<endl; return 0; }
|