【HDU3081】Marriage Match II (二分+最大流)
DescriptionPresumably, 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 3Sample Output
2
n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她朋友能配对的男生配对。
每次配对,每个女生都要跟不同的男生配对且每个女生都能配到对。问最多能配对几轮。(n<=100)
【分析】
二分答案,因为有单调性。然后直接最大流了。输入那里要用并查集。
注意不能直接跑最大流哦,(因为你不知道跑完最大流之后ans是啥啊)
哇塞~TLE了一辈子,dinic中几个不起眼的优化也能是可以挽救你于水火之中啊。比如下面的加粗部分:
int find_flow(int x,int flow)
{
if(x==ed) return flow;
int now=0;
for(int i=first[x];i;i=t[i].next) if(t[i].f>0)
{
int y=t[i].y;
if(dis[y]==dis[x]+1)
{
int a=find_flow(y,mymin(t[i].f,flow-now));
t[i].f-=a;
t[t[i].o].f+=a;
now+=a;
if(now==flow) break;
}
}
if(now==0) dis[x]=-1;
return now;
}
我这个傻逼还因为数组开小了TLE了好一阵子。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 210
#define Maxm 10010
#define INF 0xfffffff int fa[Maxn],first[Maxn],dis[Maxn];
bool map[Maxn][Maxn]; struct node
{
int x,y,f,o,next;
};
node t[*Maxm],tt[*Maxm];int len; int st,ed;
int n,m,k; int mymin(int x,int y) {return x<y?x:y;} void ins(int x,int y,int f)
{
tt[++len].x=x;tt[len].y=y;tt[len].f=f;
tt[len].next=first[x];first[x]=len;tt[len].o=len+;
tt[++len].x=y;tt[len].y=x;tt[len].f=;
tt[len].next=first[y];first[y]=len;tt[len].o=len-;
} int ffind(int x)
{
if(fa[x]!=x) fa[x]=ffind(fa[x]);
return fa[x];
} queue<int > q;
bool bfs()
{
while(!q.empty()) q.pop();
memset(dis,-,sizeof(dis));
q.push(st);dis[st]=;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=first[x];i;i=t[i].next) if(t[i].f>)
{
int y=t[i].y;
if(dis[y]==-)
{
dis[y]=dis[x]+;
q.push(y);
}
}
}
if(dis[ed]==-) return ;
return ;
} int find_flow(int x,int flow)
{
if(x==ed) return flow;
int now=;
for(int i=first[x];i;i=t[i].next) if(t[i].f>)
{
int y=t[i].y;
if(dis[y]==dis[x]+)
{
int a=find_flow(y,mymin(t[i].f,flow-now));
t[i].f-=a;
t[t[i].o].f+=a;
now+=a;
if(now==flow) break;
}
}
if(now==) dis[x]=-;
return now;
} int max_flow()
{
int ans=;
while(bfs())
ans+=find_flow(st,INF);
return ans;
} bool check(int x)
{
for(int i=;i<=len;i++) t[i]=tt[i];
for(int i=len-*n+;i<=len;i+=) t[i].f=x;
return max_flow()==n*x;
} void finda()
{
int l=,r=n*n;
while(l<r)
{
int mid=(l+r+)>>;
if(check(mid)) l=mid;
else r=mid-;
}
printf("%d\n",l);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
len=;
memset(first,,sizeof(first));
memset(map,,sizeof(map));
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
map[x][y]=;
}
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
fa[ffind(x)]=ffind(y);
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++) if(ffind(i)==ffind(j))
{
for(int k=;k<=n;k++) if(map[j][k])
map[i][k]=;
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++) if(map[i][j])
ins(i,j+n,);
st=*n+;ed=st+;
for(int i=;i<=n;i++) ins(st,i,INF);
for(int i=;i<=n;i++) ins(i+n,ed,INF);
finda();
}
return ;
}
[HDU3081]
所以现在我的数组是能开多大开多大。
2016-05-29 15:09:33
【HDU3081】Marriage Match II (二分+最大流)的更多相关文章
- hdu3081 Marriage Match II(二分+并查集+最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Marriage Match II(二分+并查集+最大流,好题)
Marriage Match II http://acm.hdu.edu.cn/showproblem.php?pid=3081 Time Limit: 2000/1000 MS (Java/Othe ...
- hdu3081 Marriage Match II(最大流)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Marriage Match II Time Limit: 2000/1000 M ...
- HDU 3081 Marriage Match II 二分 + 网络流
Marriage Match II 题意:有n个男生,n个女生,现在有 f 条男生女生是朋友的关系, 现在有 m 条女生女生是朋友的关系, 朋友的朋友是朋友,现在进行 k 轮游戏,每轮游戏都要男生和女 ...
- HDU 3081 Marriage Match II (二分+并查集+最大流)
题意:N个boy和N个girl,每个女孩可以和与自己交友集合中的男生配对子;如果两个女孩是朋友,则她们可以和对方交友集合中的男生配对子;如果女生a和女生b是朋友,b和c是朋友,则a和c也是朋友.每一轮 ...
- hdu3081 Marriage Match II
新年第一篇,又花了一早上,真是蠢啊! 二分+网络流 之前对于讨论哪些人是朋友的时候复杂度过高 直接n3的暴力虽然看起来复杂度高,其实并不是每次都成立 #include<bits/stdc++.h ...
- HDU 3081 Marriage Match II(二分法+最大流量)
HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...
随机推荐
- PureMVC(JS版)源码解析(三):Observer类
上一篇博客中,我们讲到了Notification类(消息类),Notification(消息)是连接观察者(observer)和通知者(notifier)之间的桥梁.这一篇博客,主要是在代 ...
- SDWebImage 源码阅读分享
SDWebImage 源码阅读分享 疑问列表 SDWebImage 整体框架图,主要的类包含哪些 SDWebImage 如何进行缓存管理,过期失效策略,缓存更新 SDWebImage 如何多线程处理的 ...
- js基础知识之_函数
javascript函数 函数概念 将完成某一特定功能的代码集合起来,可以重复使用 白话函数理解-函数就是一个工厂,帮大家实现某一个功能 优点 -时程序更加简洁 -逻辑更有条例 -调用方便 -维护更加 ...
- C语言开发环境配置
链接:http://pan.baidu.com/s/1qWkpD72 密码:zhig 将解压包直接解压放在C盘下. 右击我的电脑,点属性—>高级—>环境变量然后在PATH里加入C:\Min ...
- ecshop 函数列表大全
lib_time.phpgmtime() P: 获得当前格林威治时间的时间戳 /$0server_timezone() P: 获得服务器的时区 /$0local_mktime($hour = NULL ...
- umask默认权限分配
umask默认权限分配的命令 当我们登录系统之后创建一个文件总是有一个默认权限的,那么这个权限是怎么来的呢?这就是umask干的事情.umask设置了用户创建文件的默认 权限,它与chmod的效果刚好 ...
- C#后台程序与HTML页面中JS方法互调
此方法适用于 C#中嵌入WebBrowser(浏览器) 通过浏览器中加载的页面与C#的后台代码进行交互. 一.C#程序 1.在C#窗体中添加WebBrowser(浏览器),将页面的URL添加到浏览器中 ...
- C++静态成员函数访问非静态成员的几种方法
大家都知道C++中类的成员函数默认都提供了this指针,在非静态成员函数中当你调用函数的时候,编译器都会“自动”帮你把这个this指针加到函数形参里去.当然在C++灵活性下面,类还具备了静态成员和静态 ...
- 为什么我们需要使用 touch 命令
为什么我们需要使用 touch 命令 既然 touch 命令描述的是改变时间戳,那么我们可能会想为什么我们需要去改变时间戳呢?这个问题会引发我们的深思.然而,我想有个理由可以解释为什么我们需要使用它. ...
- JavaScript 输入自动完成插件
作为web开发的一员,应该都不陌生,信息处理时,很多时候需要根据用户的输入实时反馈查询结果供其选择,这给了用户很好的人机交互体验,在各大门户网站上已经被使用的很成熟了,最近项目中用到此功能,网上有很多 ...