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
Problem Description
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?

 
Input
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.
 
Output
For each case, output a number in one line. The maximal number of Marriage Match the children can play.
 
Sample Input
1
4 5 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
 
Sample Output
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;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

HDU--3081--Marriage Match II--最大匹配,匈牙利算法的更多相关文章

  1. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

  2. HDU 3081 Marriage Match II(二分法+最大流量)

    HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...

  3. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  4. HDU 3081 Marriage Match II 二分 + 网络流

    Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...

  5. HDU 3081 Marriage Match II 最大流OR二分匹配

    Marriage Match IIHDU - 3081 题目大意:每个女孩子可以和没有与她或者是她的朋友有过争吵的男孩子交男朋友,现在玩一个游戏,每一轮每个女孩子都要交一个新的男朋友,问最多可以玩多少 ...

  6. HDU - 3081 Marriage Match II 【二分匹配】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意 有n对男女 女生去选男朋友 如果女生从来没和那个男生吵架 那么那个男生就可以当她男朋友 女 ...

  7. HDU 3081 Marriage Match II

    二分图的最大匹配+并查集 每次匹配完之后,删除当前匹配到的边. #include<cstdio> #include<cstring> #include<cmath> ...

  8. HDU 3081 Marriage Match II (二分+网络流+并查集)

    注意 这题需要注意的有几点. 首先板子要快,尽量使用带当前弧优化的dinic,这样跑起来不会超时. 使用弧优化的时候,如果源点设置成0,记得将cur数组从0开始更新,因为有的板子并不是. 其次这题是多 ...

  9. HDU 3081 Marriage Match II (二分+并查集+最大流)

    题意:N个boy和N个girl,每个女孩可以和与自己交友集合中的男生配对子;如果两个女孩是朋友,则她们可以和对方交友集合中的男生配对子;如果女生a和女生b是朋友,b和c是朋友,则a和c也是朋友.每一轮 ...

  10. hdu 2063 过山车 (最大匹配 匈牙利算法模板)

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最 ...

随机推荐

  1. Virtualbox mouse move in and out and file share with windows

    How to use Virstalbox to share files with Linux and Windows, and to move the mouse in and out Virtua ...

  2. C++学习笔记10-面向对象

    1.  面向对象的程序设计是基于三个基本概念:数据抽象.继承和动态绑定. 在C++ 在,凭借一流的数据抽象,随着一类从一个类派生还继承:派生类的成员继承基类.决定是使用基类中定义的函数还是派生类中定义 ...

  3. spring-bean属性配置解析

    autowire属性值有 byName 根据Bean定义时的“id"属性上指定的别名与Setter名称是否一致进行自动装配 byType 根据PoJo的setXXX()方法所接受的类型判断b ...

  4. 由“Jasperrpeorts 4.1.2升级到5.1.2对flex项目的解析”到AS3 带命名空间的XML的操作

    原文同步至:http://www.waylau.com/from-jasperrpeorts-4-1-2-upgraded-to-5-1-2-parsing-of-flex-projects-to-t ...

  5. hdu1876(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1876 题意:问机器人到达终点的过程中最多有几次完全消耗完能量,消耗完这么多次能量的方式有几种. 分析: ...

  6. MVAPI第一个版本架构图

    MVAPI采用矢量与栅格结合的方式进行移动地图的显示. 进过几个月,目前终于可以完成基本的地图显示及操作功能.还有待实现的是各种性能及效果优化.3D地物等. 发一个1.0的架构图留存一下.(虽然目前还 ...

  7. STL 源代码剖析 算法 stl_numeric.h -- copy

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie copy //唯一对外接口 /*------------------------------ ...

  8. Java学习文件夹

    每天进步一点点,先研究一门语言深入研究下去.

  9. Top 10 Mistakes Java Developers Make(转)

    文章列出了Java开发者最常犯的是个错误. 1.将数组转换为ArrayList 为了将数组转换为ArrayList,开发者经常会这样做: ? 1 List<String> list = A ...

  10. ecshop 浏览历史样式的修改

    ecshop的浏览历史的样式,例如我修改的是只让浏览历史显示浏览历史的商品名称 而不显示浏览历史的商品的价格和图片 首先找到要修改 的文件includes\lib_insert.php 找到函数fun ...