Marriage Match II

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

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

 
起初做法:
S-girl和boy-T连边,容量n;
girl和所有能匹配的boy连边,容量1;
然后跑一次最大流,答案为S-girl和boy-T的边中流量的最小值。
 
但是,发现反例:
1
3 4 0
1 2
2 1
2 3
3 2
答案应该是0,但用上述方法答案为1。
 
所以需要二分答案,即二分S-girl和boy-T连边的容量mid,建图跑最大流,若mid*n == maxflow,表示可以进行mid轮游戏,继续二分。
 
 //2017-08-25
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#define mid ((l+r)>>1) using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int next, to, w;
}edge[M]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].w = ;
edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} struct Dinic{
int level[N], S, T;
void init(int _S, int _T){
S = _S;
T = _T;
tot = ;
memset(head, -, sizeof(head));
}
bool bfs(){
queue<int> que;
memset(level, -, sizeof(level));
level[S] = ;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(level[v] == - && w > ){
level[v] = level[u]+;
que.push(v);
}
}
}
return level[T] != -;
}
int dfs(int u, int flow){
if(u == T)return flow;
int ans = , fw;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(!w || level[v] != level[u]+)
continue;
fw = dfs(v, min(flow-ans, w));
ans += fw;
edge[i].w -= fw;
edge[i^].w += fw;
if(ans == flow)return ans;
}
if(ans == )level[u] = ;
return ans;
}
int maxflow(){
int flow = ;
while(bfs())
flow += dfs(S, INF);
return flow;
}
}dinic; bool G[N][N];
int T, n, m, f;
int never_quarreled[M][], friends[M][];
void build_graph(int cap){
int s = , t = *n+;
dinic.init(s, t);
memset(G, , sizeof(G));
int a, b;
for(int i = ; i < m; i++){
a = never_quarreled[i][];
b = never_quarreled[i][];
add_edge(a, n+b, );
G[a][b] = ;
}
for(int i = ; i < f; i++){
a = friends[i][];
b = friends[i][];
for(int i = head[a]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!G[b][v-n] && v != s && v != t){
add_edge(b, v, );
G[b][v-n] = ;
}
}
for(int i = head[b]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!G[a][v-n] && v != s && v != t){
add_edge(a, v, );
G[a][v-n] = ;
}
}
}
for(int i = ; i <= n; i++){
add_edge(s, i, cap);
add_edge(n+i, t, cap);
}
} int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputN.txt", "r", stdin);
cin>>T;
while(T--){
cin>>n>>m>>f;
for(int i = ; i < m; i++)
cin>>never_quarreled[i][]>>never_quarreled[i][];
for(int i = ; i < f; i++)
cin>>friends[i][]>>friends[i][];
int l = , r = n, ans = ;
while(l <= r){
build_graph(mid);
int flow = dinic.maxflow();
if(flow == mid*n){
ans = mid;
l = mid+;
}else{
r = mid-;
}
}
cout<<ans<<endl;
}
return ;
}

HDU3081(KB11-N 二分答案+最大流)的更多相关文章

  1. BZOJ 1570: [JSOI2008]Blue Mary的旅行( 二分答案 + 最大流 )

    二分答案, 然后对于答案m, 把地点分成m层, 对于边(u, v), 第x层的u -> 第x+1层的v 连边. 然后第x层的u -> 第x+1层的u连边(+oo), S->第一层的1 ...

  2. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

    一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...

  3. BZOJ 1305 CQOI2009 dance跳舞 二分答案+最大流

    题目大意:给定n个男生和n个女生,一些互相喜欢而一些不.举行几次舞会,每次舞会要配成n对.不能有同样的组合出现.每一个人仅仅能与不喜欢的人跳k次舞,求最多举行几次舞会 将一个人拆成两个点.点1向点2连 ...

  4. BZOJ2547 CTSC2002玩具兵(最短路径+二分答案+最大流)

    先不考虑只有一个显得有些特殊的天兵. 可以发现超能力的作用实质上是使兵更换职业.每一个兵到达某个位置最少需要更换职业的次数是彼此独立的,因为如果需要某两人互换职业可以使他们各自以当前职业到达需要到的地 ...

  5. Gym - 101908G 二分答案+最大流

    After the end of the truck drivers' strike, you and the rest of Nlogônia logistics specialists now h ...

  6. 紫书 习题 11-10 UVa 12264 (二分答案+最大流)

    书上写的是UVa 12011, 实际上是 12264 参考了https://blog.csdn.net/xl2015190026/article/details/51902823 这道题就是求出一种最 ...

  7. luoguP1401 城市(二分答案+最大流)

    题意 N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最长的边的长度最小,边不能重复 ...

  8. Marriage Match II 【HDU - 3081】【并查集+二分答案+最大流】

    题目链接 一开始是想不断的把边插进去,然后再去考虑我们每次都加进去边权为1的边,直到跑到第几次就没法继续跑下去的这样的思路,果不其然的T了. 然后,就是想办法咯,就想到了二分答案. 首先,我们一开始处 ...

  9. bzoj 3993 星际战争 - 二分答案 - 最大流

    3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai.当一个巨型机器人的装甲值减少到0或者 ...

随机推荐

  1. 栈(链式栈)----C语言

    链式栈:就是一种操作受限的单向链表,对单向链表还不了解的可先看一下之前的一篇关于单向链表的随笔,链表(单向链表的建立.删除.插入.打印),理解了单向链表后再来看链式栈就比较轻松了 链式栈的操作一般含有 ...

  2. cad2016卸载/安装失败/如何彻底卸载清除干净cad2016注册表和文件的方法

    cad2016提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装cad2016失败提示cad2016安装未完成,某些产品无法安装,也有时候想重新安装cad2016 ...

  3. Redis---quickList(快速列表)

    1. 概述 考虑到链表的附加空间相对太高,prev 和 next 指针就要占去 16 个字节 (64bit 系统的指针是 8 个字节),另外每个节点的内存都是单独分配,会加剧内存的碎片化,影响内存管理 ...

  4. Mac 安装mongodb

    http://blog.csdn.net/u010311313/article/details/46948995 1.前往官网下载MongoDB压缩包 2.将下载好的压缩包解压,将解压出的文件夹下的内 ...

  5. supervisor 管理uwsgi 进程

    Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动 重启.它是通过fork/exec的方式把这些被管理的进 ...

  6. (转)30 个实例详解 TOP 命令

    原文:http://blog.jobbole.com/112873/?utm_source=blog Linux中的top命令显示系统上正在运行的进程.它是系统管理员最重要的工具之一.被广泛用于监视服 ...

  7. android开发学习——day4

    自己手动创建空活动,创建和加载布局,效果:界面中出现靠上对齐的button 在活动中使用Toast,效果:对点击按钮做出响应 在活动中使用menu,效果:界面中出现菜单,并且点击对应选项会有响应 De ...

  8. 全网最详细的大数据集群环境下多个不同版本的Cloudera Hue之间的界面对比(图文详解)

    不多说,直接上干货! 为什么要写这么一篇博文呢? 是因为啊,对于Hue不同版本之间,其实,差异还是相对来说有点大的,具体,大家在使用的时候亲身体会就知道了,比如一些提示和界面. 安装Hue后的一些功能 ...

  9. 拥抱了IDEA却发现再也回不去Eclipse...

    一.背景 还记得去年入职的时候,发现很多同事都在用Intellij IDEA,其实在那之前都已经接触过,只不过没有在开发中实际应用而已. 这时候我下定决心要拥抱IDEA了,尤其被它酷酷的黑色主题所吸引 ...

  10. JAVA中 XML与数据库互转 学习笔记三

    要求 必备知识 JAVA基础知识,XML基础知识,数据库的基本操作. 开发环境 MyEclipse10/MySql5.5 资料下载 源码下载   数据库在数据查询,修改,保存,安全等方面与其他数据处理 ...