hdu 3277(二分+最大流+拆点+离线处理+模板问题...)
Marriage Match III
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1971 Accepted Submission(s): 583
you all have known the question of stable marriage match. A girl will
choose a boy; it is similar as the ever game of play-house . What a
happy time as so many friends play 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. As 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. On the other hand, in order to play more times of marriage
match, every girl can accept any K boys. If a girl chooses a boy, the
boy must accept her unconditionally whether they had quarreled before or
not.
Now, here is the question for you, how many rounds can these 2n kids totally play this game?
Each
test case starts with three integer n, m, K and f in a line
(3<=n<=250, 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.
4 5 1 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXNODE = ;
const int MAXEDGE = MAXNODE*MAXNODE; typedef int Type;
const Type INF = 0x3f3f3f3f; struct Edge
{
int u, v;
Type cap, flow;
Edge() {}
Edge(int u, int v, Type cap, Type flow)
{
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
}
}; struct Dinic
{
int n, m, s, t;
Edge edges[MAXEDGE];
int first[MAXNODE];
int next[MAXEDGE];
bool vis[MAXNODE];
Type d[MAXNODE];
int cur[MAXNODE];
vector<int> cut; void init(int n)
{
this->n = n;
memset(first, -, sizeof(first));
m = ;
}
void add_Edge(int u, int v, Type cap)
{
edges[m] = Edge(u, v, cap, );
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, , );
next[m] = first[v];
first[v] = m++;
} bool bfs()
{
memset(vis, false, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = ;
vis[s] = true;
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i = first[u]; i != -; i = next[i])
{
Edge& e = edges[i];
if (!vis[e.v] && e.cap > e.flow)
{
vis[e.v] = true;
d[e.v] = d[u] + ;
Q.push(e.v);
}
}
}
return vis[t];
} Type dfs(int u, Type a)
{
if (u == t || a == ) return a;
Type flow = , f;
for (int &i = cur[u]; i != -; i = next[i])
{
Edge& e = edges[i];
if (d[u] + == d[e.v] && (f = dfs(e.v, min(a, e.cap - e.flow))) > )
{
e.flow += f;
edges[i^].flow -= f;
flow += f;
a -= f;
if (a == ) break;
}
}
return flow;
} Type Maxflow(int s, int t)
{
this->s = s;
this->t = t;
Type flow = ;
while (bfs())
{
for (int i = ; i < n; i++)
cur[i] = first[i];
flow += dfs(s, INF);
}
return flow;
}
void MinCut()
{
cut.clear();
for (int i = ; i < m; i += )
{
if (vis[edges[i].u] && !vis[edges[i].v])
cut.push_back(i);
}
}
} gao;
const int N = ;
bool vis[N][N];
int father[N];
int girl[N*N],boy[N*N]; ///这里m是属于 (0,n*n]的
int n,m,k,f,src,des;
int _find(int x)
{
if(father[x]!=x)
{
father[x] = _find(father[x]);
}
return father[x];
}
void build(int c)
{
gao.init(*n+);
for(int i=; i<=n; i++)
{
gao.add_Edge(src,i,c);
gao.add_Edge(i,n+i,k);
gao.add_Edge(*n+i,des,c);
}
for(int i=; i<=n; i++)
{
for(int j=*n+; j<=*n; j++)
{
if(vis[i][j]) gao.add_Edge(i,j,);
else gao.add_Edge(n+i,j,);
}
}
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d%d%d",&n,&m,&k,&f);
memset(vis,false,sizeof(vis));
src = ,des = *n+;
for(int i=; i<=n; i++) father[i] = i;
/* for(int i=1;i<=m;i++){ //TLE
int u,v;
scanf("%d%d",&u,&v);
v+=2*n;
vis[u][v] = true;
}*/
for(int i=; i<=m; i++)
{
scanf("%d%d",&girl[i],&boy[i]);
boy[i]+=*n;
}
for(int i=; i<=f; i++)
{
int u,v;
scanf("%d%d",&u,&v);
int a = _find(u),b = _find(v);
if(a!=b)
father[a] = b;
}
for(int i=; i<=m; i++)
{
vis[_find(girl[i])][boy[i]] = true;
}
for(int i=; i<=n; i++) ///预处理所有关系
{
for(int j=*n+; j<=*n; j++)
{
if(vis[i][j]) continue;
if(vis[_find(i)][j]) vis[i][j] = true;
}
}
/*for(int i=1;i<=n;i++){ //TLE
for(int j=1;j<=n;j++){
if(_find(i)==_find(j)){
for(int k=2*n+1;k<=3*n;k++){
if(vis[i][k]||vis[j][k]) vis[i][k] = vis[j][k] = 1;
}
}
}
}*/
int l = ,r = n,ans = ;
while(l<=r)
{
int mid = (l+r)>>;
build(mid);
if(gao.Maxflow(src,des)==mid*n)
{
ans = mid;
l = mid+;
}
else r = mid-;
}
printf("%d\n",ans);
}
return ;
}
hdu 3277(二分+最大流+拆点+离线处理+模板问题...)的更多相关文章
- poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点
题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...
- poj--2391--Ombrophobic Bovines(floyd+二分+最大流拆点)
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u ...
- HDU 4289 Control(最大流+拆点,最小割点)
题意: 有一群恐怖分子要从起点st到en城市集合,你要在路程中的城市阻止他们,使得他们全部都被抓到(当然st城市,en城市也可以抓捕).在每一个城市抓捕都有一个花费,你要找到花费最少是多少. 题解: ...
- HDU 3277 Marriage Match III(二分+最大流)
HDU 3277 Marriage Match III 题目链接 题意:n个女孩n个男孩,每一个女孩能够和一些男孩配对,此外还能够和k个随意的男孩配对.然后有些女孩是朋友,满足这个朋友圈里面的人.假设 ...
- poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap
poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...
- 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 二分图匹配+并查集 OR 二分+最大流
二分+最大流: 1 //题目大意:有编号为1~n的女生和1~n的男生配对 2 // 3 //首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 4 // 5 //然后输入f组,c,d表示 ...
- hdu4560 不错的建图,二分最大流
题意: 我是歌手 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Subm ...
- hdu 4024 二分
转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html 一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...
随机推荐
- bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)
这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...
- 【二分】【P1314】 【NOIP2011D2T2】聪明的质监员
传送门 Description 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 \(n\) 个矿石,从 \(1\) 到 \(n\) 逐一编号,每个矿石都有自己的重量 \(w_i\) ...
- 论C++11 中vector的N种遍历方法
随着C++11标准的出现,C++标准添加了许多有用的特性,C++代码的写法也有比较多的变化. vector是经常要使用到的std组件,对于vector的遍历,本文罗列了若干种写法. (注:本文中代码为 ...
- 分割png图片
/*** * 分割图片 */public function cut_img(){ $filename = 'site_upload/form_file_forinput/20180313/201803 ...
- jQuery图表插件Flot中文文档
转载自:http://www.itivy.com/ivy/archive/2011/6/4/jquery-flot-chinese-doc.html 最近正在使用JQuery的flot进行画图,但是这 ...
- 在MVC5中使用Ninject 依赖注入
各大主流.Net的IOC框架性能测试比较 : http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html 使用NuGet ...
- Mybatis 使用技巧总结
9月 11, 2014 | Nix.Huang 目录: 1.区分 #{} 和 ${}的不同应用场景 2.spring环境用mybatis-spring 的接口而不是Mybatis的原生接口 3.返回M ...
- 使用pipenv管理python项目
入门 首先使用pip安装Pipenv及其依赖项, pip install pipenv 1 然后将目录更改为包含你的Python项目的文件夹,并启动Pipenv, cd my_project pipe ...
- Linux 下访问Windows共享目录
5.安装cifs-utils软件包 # yum install cifs-utils 貌似可以不用安装这个包. 6.在Linux下创建一个挂载点 # mkdir /mnt/Windows 7.挂载W ...
- [uva11137]立方数之和·简单dp
小水题再来一发 给定一个正整数n<=1e4,求将n写成若干个正整数立方和的方法数 典型的多阶段模型 f[i][j]表示当前用到1~i的数,累计和为j的方案数. #include<cstdi ...