转载请注明出处: 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. GetClientRect()和GetWindowRect()

    GetClientRect()   是得到客户区坐标系下客户区的RECT GetWindowRect()是得到屏幕坐标系下整个窗口的RECT GetSystemMetrics()是获得屏幕分辨率大小 ...

  2. DOM 节点属性

    DOM 节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeT ...

  3. 怎么在网页中加入ICO图标

    1.首先制作一个16x16的icon图标,命名为cssbbs.ico(这里的名字可以随便改!),放在根目录下.2.然后将下面的代码嵌入head区:<link rel="icon&quo ...

  4. python——lambda

    一lambda函数基础 1.lambda函数为匿名函数,即没有具体的函数名,而def函数创建的函数有函数名. >>> def foo(): return 'test' #命名为foo ...

  5. 1169 二叉树遍历(XCOJ DFS)

    给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度≤8). 样例输入 BADC BDCA 样例输出 ABCD #include <iostream> ...

  6. Django RESTful API 设计指南

    网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...

  7. 分治算法求乘方a^b 取余p(divide and conquer)

    传统的计算方法为循环n个a相乘.时间复杂度为O(n). 如用分治算法,效率可提升至O(lgn). 结合recursive有 double pow(int a, int n){ ) ; ) return ...

  8. windows安装配置mysql-5.7.13-winx64方法

    1.mysql-5.7.13-winx64.zip下载 官方下载地址:http://dev.mysql.com/downloads/mysql/ 2.解压到D:\mysql\mysql-5.7.13- ...

  9. LeetCode_Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  10. mutate 转换

    zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat geoip.conf input {stdin {} } filter { geoip { ...