转载请注明出处: 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. 练习—单链表—Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  2. ldd命令--查看命令依赖的库文件

    .在制作自己的发行版时经常需要判断某条命令需要哪些共享库文件的支持,以确保指定的命令在独立的系统内可以可靠的运行:在Linux环境下通过ldd命令即可实现,在终端下执行:ldd /bin/ls //l ...

  3. C和C++的学习过程总结

    总是被同学们问到,如何学习C和C++才不茫然,才不是乱学,想了一下,这里给出一个总的回复. 一家之言,欢迎拍砖哈. 1.可以考虑先学习C. 大多数时候,我们学习语言的目的,不是为了成为一个语言专家,而 ...

  4. 遍历ArrayList与LinkedList,使用FOR与迭代器的区别

    网上结论: 如果是链表结构的集合,如LinkedList,则使用迭代器遍历,速度会更快(顺序获取). 如果是数组结构的,如ArrayList,则使用for循环会更快(随机获取) 测试代码: packa ...

  5. hdu 2807 The Shortest Path

    http://acm.hdu.edu.cn/showproblem.php?pid=2807 第一次做矩阵乘法,没有优化超时,看了别人的优化的矩阵乘法,就过了. #include <cstdio ...

  6. LeetCode_Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  7. mysql Event、存储过程、表命令

    Mysql事件调度器(Event Scheduler)类似于定时器,可以在某一个时间点执行一个SQL语句或一个语句块(BEGIN ... END):或者每隔固定间隔重复执行.类似于Linux下的cro ...

  8. [置顶] java ant 配置及构建项目

      Ant是一种基于Java的构建工具.Ant文件是配置构建目标过程的XML文件,也称为Ant脚本.                     (因为对这个不是很了解,所以用词方面可能于个人的理解有偏差 ...

  9. 使用itextsharp创建PDF文档——图片集合

    文档管理系统中 ,扫描模块将文档或证件扫描后.为了便于保存多个图片,拟将多个图片生成一个PDF文档进行保存. 这里我们就需要PDF生成工具了.你可以在这里下载.PDFCreator 主要使用了开源工具 ...

  10. 04747_Java语言程序设计(一)_第6章_图形界面设计(二)

    例6.1声明一个面板子类,面板子类对象有3个选择框. class Panel1 extends JPanel { JCheckBox box1, box2, box3; Panel1() { box1 ...