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 一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...
随机推荐
- 2016多校联合训练1 B题Chess (博弈论 SG函数)
题目大意:一个n(n<=1000)行,20列的棋盘上有一些棋子,两个人下棋,每回合可以把任意一个棋子向右移动到这一行的离这个棋子最近的空格上(注意这里不一定是移动最后一个棋子),不能移动到棋盘外 ...
- CentOS7搭建 Hadoop + HBase + Zookeeper集群
摘要: 本文主要介绍搭建Hadoop.HBase.Zookeeper集群环境的搭建 一.基础环境准备 1.下载安装包(均使用当前最新的稳定版本,截止至2017年05月24日) 1)jdk-8u131 ...
- httpclient post请求带参数返回数据乱码问题解决
客户端代码: //带参数的post请求 @Test public void doPostWithParam() throws Exception { CloseableHttpClient httpC ...
- 关于string::size_type
size_type其实是string模板类定义的一种类型之一,它与size_of的用法相似,只是它根据存储的类型返回字符串的长度.对于 string具体化,将根据char返回字符串的长度,在这种情况下 ...
- UVA10766:Organising the Organisation(生成树计数)
Organising the Organisation 题目链接:https://vjudge.net/problem/UVA-10766 Description: I am the chief of ...
- Problem B. Harvest of Apples 莫队求组合数前缀和
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- 牛客328B Rabbit的工作(1)
传送门:https://ac.nowcoder.com/acm/contest/328/B 题意:给你n和k,和一个长度为n的字符串,字符串中的0表示休息,1表示工作,连续工作会损耗连续的体力值,从1 ...
- 51Nod 1003 阶乘后面0的数量 | 思维
题意:n的阶乘后面0的个数,如果直接算出阶乘再数0的数量一定会超时的. 因为10=2*5,所以求出5贡献的次数就行. #include "bits/stdc++.h" using ...
- 动态规划:LIS
题目中的严格二字,表示的意思是不允许≥或者是≤的情况出现,只允许>的情况以及<的情况 经典问题是NOIP合唱队形,在这个题目中,既求了最长上升子序列,也求了最长下降子序列 其最终的结果由两 ...
- 选择Asp for javascript,非.net。
写Web类型的程序我使用过的服务器端语言有C#和Python.PHP稍Copy过几段代码,Asp很早听说过.C#我主要用来写Web服务比较多,C#这种强类型语言写Web不太爽,特别构造复杂的JSON数 ...