HDU--3081--Marriage Match II--最大匹配,匈牙利算法
2015-10-17 17:51
483 查看
Marriage Match II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1608 Accepted Submission(s): 566
[align=left]Problem Description[/align]
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing together. And
it is normal that a fight or a quarrel breaks out, but we will still play together after that, because we are kids.
Now, there are 2n kids, n boys numbered from 1 to n, and n girls numbered from 1 to n. you know, ladies first. So, every girl can choose a boy first, with whom she has not quarreled, to make up a family. Besides, the girl X can also choose boy Z to be her boyfriend
when her friend, girl Y has not quarreled with him. Furthermore, the friendship is mutual, which means a and c are friends provided that a and b are friends and b and c are friend.
Once every girl finds their boyfriends they will start a new round of this game—marriage match. At the end of each round, every girl will start to find a new boyfriend, who she has not chosen before. So the game goes on and on.
Now, here is the question for you, how many rounds can these 2n kids totally play this game?
[align=left]Input[/align]
There are several test cases. First is a integer T, means the number of test cases.
Each test case starts with three integer n, m and f in a line (3<=n<=100,0<m<n*n,0<=f<n). n means there are 2*n children, n girls(number from 1 to n) and n boys(number from 1 to n).
Then m lines follow. Each line contains two numbers a and b, means girl a and boy b had never quarreled with each other.
Then f lines follow. Each line contains two numbers c and d, means girl c and girl d are good friends.
[align=left]Output[/align]
For each case, output a number in one line. The maximal number of Marriage Match the children can play.
[align=left]Sample Input[/align]
1 4 5 2 1 1 2 3 3 2 4 2 4 4 1 4 2 3
[align=left]Sample Output[/align]
2
题意:n个女生和n个男生,女生和男生没吵过架就能在一起。。
女生和女生之间仅仅要是好朋友,那么没吵过架的男生就都能在一起,女所有选择好了要在一起的男生就算一次匹配,然后再来。可是女生不能选先前选择过的男生,问最多能玩出几次这种匹配
题解:用匈牙利算法匹配,然后把匹配到的女生和男生的关系变为吵架(这样下次她就不会选择到他了)
注意:女生A和B是朋友。B和C是朋友。那么和A、B、C没吵过架的人都能被A、B、C选择
#include <iostream> #include <cstring> using namespace std; int n,map[111][111],vis[111],d[111],v[111],fa[111]; int Find(int x) //并查集——查找祖先 { return fa[x]==x?x:fa[x]=Find(fa[x]); } void Union(int x,int y) //并查集——合并两点 { x=Find(x); y=Find(y); if(x!=y)fa[x]=y; } int find(int x) //匈牙利算法 { int i; for(i=1;i<=n;i++) if(map[x][i]) { if(!vis[i]) { vis[i]=1; if(d[i]==-1||find(d[i])) { d[i]=x; return 1; } } } return 0; } int main (void) { int t,m,f,i,j,k,l,s; cin>>t; while(t--&&cin>>n>>m>>f) { memset(map,0,sizeof(map)); for(i=1;i<=n;i++) fa[i]=i; for(i=0;i<m;i++) { cin>>j>>k; map[j][k]=1; } for(i=0;i<f;i++) { cin>>k>>l; Union(k,l); //先用并查集把女生中是好朋友的弄到一堆 } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(Find(i)==Find(j) //假设i和j是好朋友 for(k=1;k<=n;k++) if(map[i][k]||map[j][k]) //假设k没和i吵过架或者没和j吵过架 map[i][k]=map[j][k]=1; //那么k能被i与j选择 s=0; while(1) { memset(d,-1,sizeof(d)); for(i=1,k=0;i<=n;i++) { memset(vis,0,sizeof(vis)); k+=find(i); //匈牙利算法记录匹配数 } if(k==n)s++; //所有都匹配了就记录为一次 else break; //不然就结束 for(i=1;i<=n;i++) //然后把匹配男女的关系处理为吵架 map[d[i]][i]=0; } cout<<s<<endl; } return 0; }
相关文章推荐
- python中的字符串
- 容易 删除排序链表中的重复元素
- android中获取拼音的类
- awk tricks
- Android SharedPreferences 首次运行引导界面
- Android_03_三种对话框
- java 利用JAX-RS快速开发RESTful 服务
- Ubuntu下ZooKeeper安装过程 (转)
- hdoj 2553 N皇后问题 【BFS】
- C++异常安全 http://www.cnblogs.com/qinfengxiaoyue/p/3713762.html
- 定制Segue - OverlaySegue
- iOS 开发常用第三方框架
- 软件测试- 测试用例之边界值分析法
- TCPl建立连接的三次握手机制分析
- 【小米笔试题】朋友圈问题-并查集解法
- win7 64位下如何安装配置mysql
- 解决Eclipse进行Android开发时logcat不显示问题-使用DDMS
- 在FPGA中嵌入8051核的详细方法与步骤
- [Android基础]正则表达式
- 【特种兵PPT教程】如何给PPT插入、删除背景音乐?