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

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<nn,0<=f<n). n means there are 2n 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

Http

HDU:https://vjudge.net/problem/HDU-3081

Source

二分图,并查集

题目大意

有2*n个点,现在前n个点与后n个点有若干对应关系,求有多少种二分图完美匹配的方案

解决思路

首先不考虑朋友关系。我们可以每次做二分图匹配时,如果是完美匹配,就删掉那些匹配边,再跑二分图匹配,方案数+1。若不是完美匹配,则说明已经无解,退出即可。

再来考虑朋友关系。因为朋友关系是可以传递的,所以我们可以用并查集来维护再同一个朋友集合中的人。那么只要这一个朋友集合内有一个人可以连到u,则这个集合中的所有人都可以连到u。

最后是图论结构的选择。因为本题数据范围不大,所以可以采用邻接矩阵的方式,同时这也可以很方便地支持删除操作,还不需要判重。

另:这里用匈牙利算法实现二分图匹配,具体可以参考这两篇博客:

http://www.cnblogs.com/SYCstudio/p/7138221.html

http://www.cnblogs.com/SYCstudio/p/7138230.html

另:本题还可以用网络流最大流的方式解决,具体请看这篇文章

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std; const int maxN=201;
const int maxM=maxN*maxN*2;
const int inf=2147483647; int n,m;
int Map[maxN][maxN];
int Match[maxN];
int use[maxN];
int Mayuri[maxN]; int Find(int x);//并查集寻找祖先同时路径压缩
bool Hungary(int u);//匈牙利算法求解二分图匹配 int main()
{
int T;
scanf("%d",&T);
while (T--)
{
memset(Map,0,sizeof(Map));//多组数据
int f;
scanf("%d%d%d",&n,&m,&f);
for (int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
Map[u][v]=1;//先连上边
}
for (int i=1;i<=n;i++)//并查集初始化
Mayuri[i]=i;
for (int i=1;i<=f;i++)
{
int u,v;
scanf("%d%d",&u,&v);//读入朋友关系
int fu=Find(u);//合并并查集
int fv=Find(v);
if (fu!=fv)
Mayuri[fu]=fv;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (Find(i)==Find(j))
for (int k=1;k<=n;k++)//如果在同一个集合中,只要有一个连了就都要连边
if (Map[i][k]==1)
Map[j][k]=1;
int Ans=0;//统计方案数
while (1)//多次求解二分图匹配
{
int matchcnt=0;//统计匹配个数
memset(Match,-1,sizeof(Match));//匈牙利算法求解最大流
for (int i=1;i<=n;i++)
{
memset(use,0,sizeof(use));
if (Hungary(i))
matchcnt++;
}
if (matchcnt<n)//当匹配数小于n时,不存在完美匹配,退出
break;
for (int i=1;i<=n;i++)//删去本次匹配中用到的边
Map[Match[i]][i]=0;
Ans++;
}
printf("%d\n",Ans);
}
return 0;
} int Find(int x)
{
if (Mayuri[x]!=x)
Mayuri[x]=Find(Mayuri[x]);
return Mayuri[x];
} bool Hungary(int u)
{
for (int i=1;i<=n;i++)
if ((Map[u][i]==1)&&(use[i]==0))
{
use[i]=1;
if ((Match[i]==-1)||(Hungary(Match[i])))
{
Match[i]=u;
return 1;
}
}
return 0;
}

HDU 3081 Marriage Match II (二分图,并查集)的更多相关文章

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

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

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

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

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

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

  4. HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

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

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

  6. Marriage Match II(二分+并查集+最大流,好题)

    Marriage Match II http://acm.hdu.edu.cn/showproblem.php?pid=3081 Time Limit: 2000/1000 MS (Java/Othe ...

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

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

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

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

  9. HDU 3081 Marriage Match II

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

随机推荐

  1. item 11: 比起private undefined function优先使用deleted function

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 如果你为其他开发者提供代码,并且你想阻止他们调用一个特定的函数,你 ...

  2. Elasticsearch学习总结 (Centos7下Elasticsearch集群部署记录)

    一.  ElasticSearch简单介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticse ...

  3. 使用Zabbix服务端本地邮箱账号发送报警邮件及指定报警邮件操作记录

    邮件报警有两种情况:1)Zabbix服务端只是单纯的发送报警邮件到指定邮箱,发送报警邮件的这个邮箱账号是Zabbix服务端的本地邮箱账号(例如:root@localhost.localdomain), ...

  4. 容斥原理I

    普利斯记号 以下以"人"代指受条件约束的元素. \(K(x)\)表示刚好\(x\)人满足条件的方案数. \(S(x)\)表示至少\(x\)人满足条件的方案数. \(C(x)\)表示 ...

  5. 树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花

    求树直径的方法在此转载一下大佬们的分析: 可以随便选择一个点开始进行bfs或者dfs,从而找到离该点最远的那个点(可以证明,离树上任意一点最远的点一定是树的某条直径的两端点之一:树的直径:树上的最长简 ...

  6. 网络:Xen理解

    Xen是由剑桥大学计算机实验室开发的一个开源项目.是一个直接运行在计算机硬件之上的用以替代操作系统的软件层,它能够在计算机硬件上并发的运行多个客户操作系统(Guest OS). 一.Xen虚拟化类型 ...

  7. 第二个spring冲刺第10天(及第二阶段总结)

    第二阶段算是结束了,第二阶段,我们实现了基本的功能,这是软件的开始页面,点击便会进入学习画面,目前学习画面还有待改善   燃尽图3 眨眼就完结了第二阶段的冲刺了,大致整体结构已经完成. 第二阶段总体是 ...

  8. Flask-论坛开发-2-Jinja2模板

    对Flask感兴趣的,可以看下这个视频教程:http://study.163.com/course/courseLearn.htm?courseId=1004091002 1. Jinja2 模板介绍 ...

  9. Minor GC vs Major GC vs Full GC

    http://www.importnew.com/15820.html https://plumbr.io/blog/garbage-collection/minor-gc-vs-major-gc-v ...

  10. Oracle 使用PDB 的情况下进行备份恢复的使用.

    1. 关于directory: pdb 需要在container 上面创建directory才可以使用 CDB里面创建的directory是会无反应. 在PDB 里面创建: cmd 之后运行 set ...