ayout: post

title: 训练指南 UVALive - 3989(稳定婚姻问题)

author: "luowentaoaa"

catalog: true

mathjax: true

tags:

- 二分图匹配

- 图论

- 训练指南


Ladies' Choice

UVALive - 3989

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const ll mod=998244353;
  5. const int maxn=1e3+50;
  6. const ll inf=1e10;
  7. const ll INF = 1000000000;
  8. const double eps=1e-5;
  9. #define bug cout<<"bbibibibbbb="<<endl;
  10. int pref[maxn][maxn],order[maxn][maxn],Next[maxn],future_husband[maxn],future_wife[maxn];
  11. queue<int>Q;
  12. void engage(int man,int woman){
  13. int m=future_husband[woman];
  14. if(m)future_wife[m]=0,Q.push(m);
  15. future_wife[man]=woman;
  16. future_husband[woman]=man;
  17. }
  18. int main()
  19. {
  20. std::ios::sync_with_stdio(false);
  21. std::cin.tie(0);
  22. std::cout.tie(0);
  23. int t;
  24. cin>>t;
  25. while(t--){
  26. int n;
  27. cin>>n;
  28. for(int i=1;i<=n;i++){
  29. for(int j=1;j<=n;j++)
  30. cin>>pref[i][j];
  31. Next[i]=1;
  32. future_wife[i]=0;
  33. Q.push(i);
  34. }
  35. for(int i=1;i<=n;i++){
  36. for(int j=1;j<=n;j++){
  37. int x;cin>>x;
  38. order[i][x]=j;
  39. }
  40. future_husband[i]=0;
  41. }
  42. while(!Q.empty()){
  43. int man=Q.front();Q.pop();
  44. int woman=pref[man][Next[man]++];
  45. if(!future_husband[woman])engage(man,woman);
  46. else if(order[woman][man]<order[woman][future_husband[woman]])engage(man,woman);
  47. else Q.push(man);
  48. }
  49. while(!Q.empty())Q.pop();
  50. for(int i=1;i<=n;i++)cout<<future_wife[i]<<endl;
  51. if(t)cout<<endl;
  52. }
  53. return 0;
  54. }

训练指南 UVALive - 3989(稳定婚姻问题)的更多相关文章

  1. Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法

    /** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...

  2. 训练指南 UVALive - 3126(DAG最小路径覆盖)

    layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...

  3. 训练指南 UVALive - 3415(最大点独立集)

    layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...

  4. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  5. 训练指南 UVALive - 5713(最小生成树 + 次小生成树)

    layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...

  6. 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树)

    layout: post title: 训练指南 UVALive - 4080(最短路Dijkstra + 边修改 + 最短路树) author: "luowentaoaa" ca ...

  7. 训练指南 UVALive - 3713 (2-SAT)

    layout: post title: 训练指南 UVALive - 3713 (2-SAT) author: "luowentaoaa" catalog: true mathja ...

  8. 训练指南 UVALive - 4287 (强连通分量+缩点)

    layout: post title: 训练指南 UVALive - 4287 (强连通分量+缩点) author: "luowentaoaa" catalog: true mat ...

  9. 训练指南 UVALive - 5135 (双连通分量)

    layout: post title: 训练指南 UVALive - 5135 (双连通分量) author: "luowentaoaa" catalog: true mathja ...

随机推荐

  1. Java 如何正确停止一个线程

    自己在做实验性小项目的时候,发现自己遇到一个问题:如何控制线程的"死亡"? 首先,如何开启一个线程呢? 最简单的代码: public class Main { public sta ...

  2. 【COGS 1873】 [国家集训队2011]happiness(吴确) 最小割

    这是一种最小割模型,就是对称三角,中间双向边,我们必须满足其最小割就是满足题目条件的互斥关系的最小舍弃,在这道题里面我们S表示文T表示理,中间一排点是每个人,每个人向两边连其选文或者选理的价值,中间每 ...

  3. 将一张表的主键(ID)重置为从1开始自增排列

    如果你有一张表,你的主键是ID,然后由于测来测去的原因,你的ID不是从1开始连续的自增了. 终于有一天,使用这张表的某个系统要导入正式数据了,强迫症这时候就表现的明显了,浑身不自在, 这时候你就需要将 ...

  4. Win10的WSL很好用呀

    WSL全名是Windows Subsystem for Linux,是win10版本号16xx之后推出的开发者功能,提供了如原生linux版的体验. 最近最新的win10春季版1803出来了,安装了看 ...

  5. 2017年研究生数学建模D题(前景目标检测)相关论文与实验结果

    一直都想参加下数学建模,通过几个月培训学到一些好的数学思想和方法,今年终于有时间有机会有队友一起参加了研究生数模,but,为啥今年说不培训直接参加国赛,泪目~_~~,然后比赛前也基本没看,直接硬刚.比 ...

  6. HDU 2844 二进制优化的多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. Dilworth定理证明

    命题:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. (离散数学结构第六版课本P245:把一个偏序集划分成具有全序的子集所需要的最少子集个数与元素在偏序下都是不可比的最大集合的基数之间有 ...

  8. 线程 ManualResetEvent 类

    Reset(): 当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时, 它调用 Reset 以将 ManualResetEvent 置于非终止状态.此线程可被视为控制 ManualRese ...

  9. JAVA多线程---好的博客资源收集

    个人笔记,备忘 1.http://blog.csdn.net/column/details/concurrency.html    兰亭风雨的专栏 2.http://lavasoft.blog.51c ...

  10. linux 服务器下入侵之后的日志清理

    1.web日志的清理:access.log 和auth.log 位置在/var/log/下面. 2.系统日志存放在:/root/.bash_history