【HDU - 3081】Marriage Match II(网络流最大流,二分+网络流)

题干:

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个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她朋友能配对的男生配对。

每次配对,每个女生都要跟不同的男生配对且每个女生都能配到对。问最多能配对几轮。

解题报告:(贴一个题解链接)

考虑用网络流做,首先女生可以与她朋友能配对的男生配对,这样需要用并查集保存他们可以配对的关系。

为了保证每一轮所有女生和男生都能匹配到,我们需要二分源点和女生、男生和汇点之间的容量k,并且需要保证满流。找到最大的满足条件的k就是答案。

解法的正确性可以用数学归纳法证明,当k=1时,转化为一个二分匹配,如果满流,就说明可以进行一轮。当k-1轮可以实现时(k-1满流),如果容量为k时满流,说明也可以实现k轮。这样就证明了正确性。

由这个解法我们也可以想到用二分匹配的方法来解决:进行二分图的最大匹配,在匹配完成后判断匹配数是否等于n,不是的话说明GAME OVER 求得答案,是的话说明游戏能完成,然后进行删边操作,再继续匹配,直到匹配数<n则得到答案。

AC代码:

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
int tot;
struct Edge {int to,ne,w;
} e[1000005 * 2];
int head[10005];
int st,ed;
int dis[10050],q[10005];//一共多少个点跑bfs,dis数组和q数组就开多大。 
void add(int u,int v,int w) {e[++tot].to=v; e[tot].w=w; e[tot].ne=head[u]; head[u]=tot;e[++tot].to=u; e[tot].w=0; e[tot].ne=head[v]; head[v]=tot;
}
bool bfs(int st,int ed) {memset(dis,-1,sizeof(dis));int front=0,tail=0;q[tail++]=st;dis[st]=0;while(front<tail) {int cur = q[front];if(cur == ed) return 1;front++;for(int i = head[cur]; i!=-1; i = e[i].ne) {if(e[i].w&&dis[e[i].to]<0) {q[tail++]=e[i].to;dis[e[i].to]=dis[cur]+1;}}}if(dis[ed]==-1) return 0;return 1;
}
int dfs(int cur,int limit) {//limit为源点到这个点的路径上的最小边权 if(limit==0||cur==ed) return limit;int w,flow=0;for(int i = head[cur]; i!=-1; i = e[i].ne) {		if(e[i].w&&dis[e[i].to]==dis[cur]+1) {w=dfs(e[i].to,min(limit,e[i].w));e[i].w-=w;e[i^1].w+=w;flow+=w;limit-=w;if(limit==0) break;}}if(!flow) dis[cur]=-1;return flow;
}
int dinic() {int ans = 0;while(bfs(st,ed)) ans+=dfs(st,0x7fffffff);return ans;
}
int v[100005],u[100005],n,m,f;
int fa[100005];
bool has[555][555];
int getf(int v) {return fa[v] == v ? v : fa[v] = getf(fa[v]); 
}
bool ok(int x) {//每次都要重新建图 tot=1;memset(head,-1,sizeof head);for(int i = 1; i<=n; i++) add(st,i,x);for(int i = 1; i<=n; i++) add(n+i,ed,x);//设为INF也可以??答:并不可以 
//	for(int i = 1; i<=m; i++) add(u[i],n+v[i],1);for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) if(has[i][j]) add(i,j+n,1);}int ans = dinic();if(x*n == ans) return 1;else return 0;
}
int main() 
{int t;cin>>t;while(t--) {scanf("%d%d%d",&n,&m,&f);memset(has,0,sizeof has);for(int i = 1; i<=n; i++) fa[i]=i;st=2*n+1,ed=st+1;for(int i = 1; i<=m; i++) {//u女v男 scanf("%d%d",u+i,v+i);has[u[i]][v[i]]=1;}for(int u,v,i = 1; i<=f; i++) {//f对女生 scanf("%d%d",&u,&v);u=getf(u),v=getf(v);fa[u]=v;}for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)if(getf(i)==getf(j))for(int k=1;k<=n;++k)if(has[i][k])has[j][k]=1;int l = 0,r = n,mid,ans=0;while(l<=r) {mid=(l+r)>>1;if(ok(mid)) l=mid+1,ans=mid;else r = mid-1;}printf("%d\n",ans);	}return 0;
}

总结:

错误想法1:S-Girl和Boy-T设成INF,然后跑一次最大流,然后答案为S-Girl和Boy-T的边中流量的最小值,为什么不行。

给一组样例:

1
3 4 0
1 2
2 1
2 3
3 2

答案应该是0,但用上述方法答案为1。

错误想法2:

考虑到n个女生和n个男生不重复完全配对最多只能进行n轮,想到可以将源点和女生、男生和汇点之间的容量赋为n,求出最大流。但是发现这样是不行的,如果有女生或者男生一直无法配对,则答案本应是0,但是最大流肯定是>0的,所以答案是不对的。这个想法错就错在没有抓住那个关键点:每一次舞会一定是男生女生全员参与,也就是抽象成:女生这边都流出1的流量总有所有的男生全部接受到,也就是男生这边接收到的流量总是等量的,不能是:1号男生接受到2流量,2号男生没有流量流过。

错误想法3:

为什么不能直接把男生和汇点的流量设置为INF?

答:这也是和错误想法2差不多相同的错误。你这样没法保证每个男生接受的流量总是等量的。

 

本来想着是把左侧的点再拆成n个点,然后对应连到右边,后来发现没啥意义,可以直接连到右边的。

不过本来还有个想法,那就是把她和她的朋友关系直接转化到左边的点(女生之间的点)相连,而不直接转化到和男生的对应关系,但是后来发现这样是不行的,因为你这样没法控制每一轮,每个女生只能和一个男生建立关系,因为你这样相当于把她俩女生当成一个完全一致的物品去看待了,但是其实她俩肯定是有区别的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440011.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IP、TCP、UDP、HTTP头部信息

IP头部信息 ip报文段格式 版本&#xff1a; 占4位&#xff0c;表明IP协议实现的版本号&#xff0c;当前一般为IPv4&#xff0c;即0100。报头长度 &#xff1a; 占4位&#xff0c;因为头部长度不固定&#xff08;Option可选部分不固定&#xff09;&#xff0c;所以需要标识…

ROS技术点滴 —— MoveIt!中的运动学插件

MoveIt!是ROS中一个重要的集成化开发平台&#xff0c;由一系列移动操作的功能包组成&#xff0c;提供运动规划、操作控制、3D感知、运动学等功能模块&#xff0c;是ROS社区中使用度排名前三的功能包&#xff0c;目前已经支持众多机器人硬件平台。 MoveIt!中的众多功能都使用插件…

1)机器学习基石笔记Lecture1:The Learning Problem

网上关于机器学习的课程有很多&#xff0c;其中最著名的是吴恩达老师的课程&#xff0c;最近又发现了NTU林轩田老师的《机器学习基石》课程&#xff0c;这门课也很好。课程总共分为4部分&#xff0c;总共分为16节课&#xff0c;今天来记录第一节课。 When Can Machines Learn?…

MMS协议

MMS格式解析 简介&#xff1a; MMS是微软的私有流媒体协议。 它的最初目的是通过网络传输多媒体广播、视频、音轨、现场直播和一系列的实时或实况材料。 MMS建立在UDP或TCP传输&#xff0f;网络层上&#xff0c;是属于应用层的。使用TCP的MMS上URL是MMS://或者MMST://&#x…

【HDU - 6118】度度熊的交易计划(最小费用可行流,网络流费用流变形 )

题干&#xff1a; 度度熊参与了喵哈哈村的商业大会&#xff0c;但是这次商业大会遇到了一个难题&#xff1a; 喵哈哈村以及周围的村庄可以看做是一共由n个片区&#xff0c;m条公路组成的地区。 由于生产能力的区别&#xff0c;第i个片区能够花费a[i]元生产1个商品&#xff0…

老王说ros的tf库

ros的tf库 为了这个题目&#xff0c;我是拿出了挤沟的精神挤时间&#xff0c;是下了功夫的&#xff0c;线性代数、矩阵论复习了&#xff0c;惯性导航里的dcm、四元数也了解了&#xff0c;刚体力学也翻了&#xff0c;wiki里的欧拉角也读了&#xff0c;tf的tutorial、paper、sou…

Apollo进阶课程 ③ | 开源模块讲解(中)

目录 1&#xff09;ISO-26262概述 2&#xff09;ISO-26262认证流程 3&#xff09;ISO-26262优点与缺陷 原文链接&#xff1a;Apollo进阶课程 ③ | 开源模块讲解&#xff08;中&#xff09; Apollo自动驾驶进阶课程是由百度Apollo联合北京大学共同开设的课程&#xff0c;邀请…

python 问题集

打开文件是报&#xff1a;UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xe9 in position 0: unexpected end of data UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0xe9:unex 在open中加入encodingunicode_escape 如&#xff1a; with open(file_n…

【HDU - 6447】YJJ's Salesman(降维dp,树状数组优化dp)

题干&#xff1a; YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination. One day, he is going to travel from city A to southeastern city B. Let us assume that A …

由浅到深理解ROS(5.1)- roslaunch 学习

oslaunch 用处&#xff1a;将多个rosnode 结合起来&#xff0c;一起运行。这样就不需要一个个的运行。 roslaunch格式 &#xff08;add_two.launch&#xff09; <launch> <arg name"a" default"1" /> <arg name"b&q…

CS231n Convolutional Neural Networks for Visual Recognition------Numpy Tutorial

源链接为&#xff1a;http://cs231n.github.io/python-numpy-tutorial/。 这篇指导书是由Justin Johnson编写的。 在这门课程中我们将使用Python语言完成所有变成任务&#xff01;Python本身就是一种很棒的通用编程语言&#xff0c;但是在一些流行的库帮助下&#xff08;numpy&…

【CodeForces - 1047C】Enlarge GCD(数学,枚举,预处理打表,思维)

题干&#xff1a; F先生有n个正整数&#xff0c;a1&#xff0c;a2&#xff0c;...&#xff0c;an 他认为这些整数的最大公约数太小了,所以他想通过删除一些整数来扩大它 您的任务是计算需要删除的最小整数数,以便剩余整数的最大公约数大于所有整数的公约数. Input 3 1 2 4…

TS解析文档

TS格式解析 简介&#xff1a; ts文件为传输流文件&#xff0c;视频编码主要格式h264/mpeg4&#xff0c;音频为acc/MP3。 ts的包是一个一个188字节的包组成&#xff0c;这188字节里面由一个0x47开头的包作为同步。 也就是说&#xff0c;如果你找到了0x47&#xff0c;如果与它相…

ROS入门之——浅谈launch

0.何为launch&#xff1f; launch&#xff0c;中文含义是启动&#xff0c;launch文件顾名思义就是启动文件&#xff0c;要说这launch文件啊&#xff0c;那还得从roslaunch说起。 相传&#xff0c;在程序猿们还没有使用roslaunch之前&#xff0c;需要手动rosrun逐个启动node&am…

2)机器学习基石笔记Lecture2:Learning to Answer Yes/No

目录 0.上节回顾 1. Perceptron Hypothesis Set 2. Perceptron Learning Algorithm(PLA)&#xff08;重点&#xff09; 3. Guarantee of PLA&#xff08;难点&#xff09; 4. Non-Separable Data 0.上节回顾 第一节课主要讲述了机器学习的定义及机器学习的过程&#xff0…

【CodeForces - 616C】The Labyrinth(bfs,并查集,STLset)

题干&#xff1a; 求每个*能够到达的格子数量&#xff0c;只有.可以走&#xff08;四个方向扩展&#xff09;&#xff0c;结果mod 10&#xff0c;替换 * 后输出。 Input The first line contains two integers n, m (1 ≤ n, m ≤ 1000) — the number of rows and co…

scapy和dpkt使用

scapy官方文档 Scapy 下载 # (临时换pip源) pip install scapy (-i https://pypi.tuna.tsinghua.edu.cn/simple/)导入 from scapy.all import *读取pcap文件&#xff0c;进行相关操作 # 读取文件 # 整个文件&#xff1a;packets&#xff1a;scapy.plist.PacketList对象 &…

Google Colab——谷歌免费GPU使用教程

Google Colab简介 Google Colaboratory是谷歌开放的一款研究工具&#xff0c;主要用于机器学习的开发和研究。这款工具现在可以免费使用。Google Colab最大的好处是给广大的AI开发者提供了免费的GPU使用&#xff01;GPU型号是Tesla K80&#xff01;你可以在上面轻松地跑例如&am…

javaBean和Servlet的区别

可以像使用一般的类一样使用JavaBean,Bean只是一种特殊的类。特殊在可以通过<jsp:useBean />调用JavaBean。而其他类,可以和一般java中一样使用。 Bean的参数中还可以指定范围, <jsp:useBean scope"application" />该Bean在服务器的JVM中将只有一个…

pyecharts简单使用

pyecharts 是一个用于生成 Echarts 图表的类库。 Echarts 是百度开源的一个数据可视化 JS 库。可以生成很多效果很棒的图表。 pycharts文档 |分割| echarts官网 本文主要介绍pycharts的简单使用 安装 # 安装 1.0.x 以上版本 &#xff08;需要python3.6及以上&#xff09; $ …