转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2410    Accepted Submission(s): 820

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
 



Author
starvae
 



Source

题意:

有n个女孩和n个男孩,有m个女孩与男孩的关系,代表女孩喜欢男孩,有f个女孩与女孩的关系,代表女孩与女孩是好朋友。

若女孩i与女孩j是好朋友,而女孩i喜欢男孩k,则女孩j也可以和男孩匹配。即女孩之间的关系是可以传递的,而男孩之间的不可以。

每对男孩与女孩之间只能匹配一次,问可以进行几轮配对。

分析:
先floyd搞好女孩之间的传递关系,然后对于可以配对的女孩与男孩之间连一条容量为1的边,然后二分答案,每次二分之后从源点向各个女孩连容量为二分值的边,从各个男孩向汇点连容量为二分值的边。

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 1010
struct edge{
int to,cap,rev;
edge(int _to,int _cap,int _rev)
{
to=_to;
cap=_cap;
rev=_rev;
}
};
const int MAX_V=;
vector<edge>G[MAX_V];
int iter[MAX_V];
int head[MAXN];
int _to[*];
int _flow[*];
int _next[*];
int level[MAX_V];
int tot=;
void add_edge(int from,int to,int cap)
{
G[from].PB(edge(to,cap,G[to].size()));
G[to].PB(edge(from,,G[from].size()-));
}
void Add(int u,int v,int f){
_to[tot]=v;
_flow[tot]=f;
_next[tot]=head[u];
head[u]=tot++;
}
void bfs(int s,int t)
{
CLR(level,-);
queue<int>q;
level[s]=;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=;i<G[u].size();i++)
{
edge &e=G[u][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[u]+;
q.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t)return f;
for(int &i=iter[v];i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>&&level[v]<level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>)
{
e.cap-=d;;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return ;
}
int Dinic(int s,int t)
{
int flow=;
for(;;)
{
bfs(s,t);
if(level[t]<)return flow;
memset(iter,,sizeof(iter));
int f;
while((f=dfs(s,t,INF))>)
{
flow+=f;
}
}
} int a[][]; int main()
{
ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--){
int n,m,f;
scanf("%d%d%d",&n,&m,&f);
tot=;
for(int i=;i<MAXN;i++)head[i]=-;
int u,v;
memset(a,,sizeof(a));
for(int i=;i<m;i++){
scanf("%d%d",&u,&v);
a[u][v+n]=;
}
for(int i=;i<f;i++){
scanf("%d%d",&u,&v);
a[u][v]=;
a[v][u]=;
}
for(int i=;i<=n+n;i++)a[i][i]=;
for(int k=;k<=n+n;k++){
for(int i=;i<=n+n;i++){
for(int j=;j<=n+n;j++){
a[i][j]=max(a[i][j],a[i][k]&a[k][j]);
}
}
}
for(int i=;i<=n;i++){
for(int j=+n;j<=n+n;j++){
if(a[i][j])Add(i,j,);
}
}
int l=,r=n;
int s=,t=*n+;
int ans=;
while(l<=r){
int mid=(l+r)>>;
for(int i=;i<=*n+;i++)G[i].clear();
for(int i=;i<=*n;i++){
int now=head[i];
while(now!=-){
add_edge(i,_to[now],_flow[now]);
now=_next[now];
}
}
for(int i=;i<=n;i++){
add_edge(s,i,mid);
add_edge(n+i,t,mid);
}
if(Dinic(s,t)==mid*n){
ans=mid;
l=mid+;
}
else r=mid-;
}
printf("%d\n",ans);
} return ;
}

代码君

hdu3081 Marriage Match II(最大流)的更多相关文章

  1. HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流

    题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    ...

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

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

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

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

  4. hdu3081 Marriage Match II(二分+并查集+最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081 题意: n个女生与n个男生配对,每个女生只能配对某些男生,有些女生相互是朋友,每个女生也可以跟她 ...

  5. hdu3081 Marriage Match II

    新年第一篇,又花了一早上,真是蠢啊! 二分+网络流 之前对于讨论哪些人是朋友的时候复杂度过高 直接n3的暴力虽然看起来复杂度高,其实并不是每次都成立 #include<bits/stdc++.h ...

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

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

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

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

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

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

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

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

随机推荐

  1. SharpZipLib 压缩文档下载

    using ICSharpCode.SharpZipLib.Zip; Response.Clear(); Response.ClearContent(); Response.ClearHeaders( ...

  2. Oracle11g R2学习系列 之七安全性

    其实,对于目前我使用的Oracle的水平来看,还达不到使用安全管理的高度,只是作为一个学习来看一下. 关于Oracle的安全管理,一般使用OEM来操作完成好了,入口是:OEM的“服务器”属性页中,选择 ...

  3. thinkPHP中省市级联下拉列表

    公共函数放置位置common文件夹下common.php文件(此段代码也可放置在要使用的控制器中) 封装的下拉列表函数代码: /** * 根据列表拼装成一个下拉列表 ADD BY CK * @para ...

  4. [转]学DSP、FPGA、ARM,哪个更有前途?

    1.这世界真是疯了,貌似有人连FPGA原理是什么都不知道就开始来学习FPGA了. 2.DSP就是一个指令比较独特的处理器.它虽然是通用处理器,但是实际上不怎么“通用”.技术很牛的人可以用DSP做一台电 ...

  5. ubuntu下查看IP Gateway DNS信息

    使用nm-tool命令 在最底下有一行: IPv4 Settings: Address: 192.168.0.166 Prefix: (255.255.255.0) Gateway: 192.168. ...

  6. Game of Life 解答

    Question According to the Wikipedia's article: "The Game of Life, also known simply as Life, is ...

  7. 基于公网smtp协议实现邮件服务器

    刚开始做邮件服务器开发,一切都是茫然的.在书上网上都很难找到一套完整的邮件服务器开发教程.在个人的摸索中碰到了很多蛋疼得问题.现终于完成了,将我的开发经验分享给大家. 开发环境:vs2012 mfc ...

  8. 【HDU1301】Jungle Roads(MST基础题)

    爽爆.史上个人最快MST的记录7分40s..一次A. #include <iostream> #include <cstring> #include <cstdlib&g ...

  9. 构建高性能WEB站点笔记二

    构建高性能WEB站点笔记 因为是跳着看的,后面看到有提到啥epoll模型,那就补充下前面的知识. 第三章 服务器并发处理能力 3.2 CPU并发计算 进程 好处:cpu 时间的轮流使用.对CPU计算和 ...

  10. shell编程笔记(1)

    shell编程: 编译器,解释器 编程语言:机器语言.汇编语言.高级语言 静态语言:编译型语言    强类型(变量)    事先转换成可执行格式    C.C++.JAVA.C#           ...