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 一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...
随机推荐
- [CQOI2011]放棋子
想到了50%吧算是. f[i][j][k]表示,前i种,占了j行k列.方案数. 发现,转移要处理:“用c个棋子,占据n行m列”的方案数. 设g[i][j][k]表示,i行j列用k个棋子占的方案数.直接 ...
- 【单调队列】【P1776】宝物筛选
传送门 Description 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物--这下小FF可发财了,嘎嘎.但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝 ...
- cmder 添加到右键菜单
管理员权限打开cmde 输入: cmder /register all 回车,OK
- Spring3 MVC 深入核心研究
[转载自 http://elf8848.iteye.com/blog/875830] 目录: 一.前言 二.核心类与接口 三.核心流程图 四.DispatcherServlet说明 五.双亲上下文的说 ...
- 禁用 nouveau 驱动
安装Nvidia显卡的官方驱动和系统自带的nouveau驱动冲突. 安装网上方法尝试了modprob.d/blacklist.conf里的各种修改,重启以后还是没有成功警用nouveau驱动 最后看见 ...
- Hdu3579 Hello Kiki
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【题解】最大公约数之和 V3 51nod 1237 杜教筛
题目传送门 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 数学题真是做的又爽又痛苦,爽在于只要推出来公式基本上就 ...
- java入门实现转换
设计思想 首先不用多说就是建立最基础的java创建,然后抛出一个异常处理来替我们检测用户的输入,这一点十分重要.然后就要进行输入工作,不必多说,网上的教程有一个Scanner的输入方法,我们引入一下. ...
- springboot-为内置tomcat设置虚拟目录
需求 项目使用springboot开发,以jar包方式部署.项目中文件上传均保存到D判断下的upload目录下. 在浏览器中输入http://localhost:8080/upload/logo_1. ...
- sublime text3常用快捷键
Ctrl+L 选择整行(按住-继续选择下行) Ctrl+KK 从光标处删除至行尾 Ctrl+Shift+K 删除整行 Ctrl+Shift+D 复制光标所在整行,插入在该行之前 Ctrl+J 合并行( ...