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

Problem Description
Presumably,
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?

 
Input
There are several test cases. First is an integer T, means the number of test cases.
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.
 
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 1 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
 
Sample Output
3
 
Author
starvae
 
这个题的题意hdu 3081   几乎一样,多了个条件,就是女生还可以任选k个自己不喜欢的男生,所以等于女生多了一个容量限制,所以不能够用二分图解了,将女生拆点,将女生 i 和女生 n+i 之间连一条容量为k的边,i女生和喜欢的男生连容量为1的边,i+n和不喜欢的男生连容量为1的边,然后二分枚举完美配对上限,但是这题还卡时间,所以我们要将输入的男女生进行离线处理,先将所有的祖先结点和男生配对,然后再去寻找其孩子结点进行配对,这样的话就可以省掉一层循环了。自己的Dinic模板过不了,在网上找了个大神模板。不想学ISAP。。
#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(二分+最大流+拆点+离线处理+模板问题...)的更多相关文章

  1. poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点

    题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...

  2. poj--2391--Ombrophobic Bovines(floyd+二分+最大流拆点)

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u ...

  3. HDU 4289 Control(最大流+拆点,最小割点)

    题意: 有一群恐怖分子要从起点st到en城市集合,你要在路程中的城市阻止他们,使得他们全部都被抓到(当然st城市,en城市也可以抓捕).在每一个城市抓捕都有一个花费,你要找到花费最少是多少. 题解: ...

  4. HDU 3277 Marriage Match III(二分+最大流)

    HDU 3277 Marriage Match III 题目链接 题意:n个女孩n个男孩,每一个女孩能够和一些男孩配对,此外还能够和k个随意的男孩配对.然后有些女孩是朋友,满足这个朋友圈里面的人.假设 ...

  5. poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap

    poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...

  6. HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流

    题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    ...

  7. HDU-3081-Marriage Match II 二分图匹配+并查集 OR 二分+最大流

    二分+最大流: 1 //题目大意:有编号为1~n的女生和1~n的男生配对 2 // 3 //首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 4 // 5 //然后输入f组,c,d表示 ...

  8. hdu4560 不错的建图,二分最大流

    题意: 我是歌手 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Subm ...

  9. hdu 4024 二分

    转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html   一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...

随机推荐

  1. 获取指定IP地址对应的物理位置

    # -*- coding: utf-8 -*- import requests def get_physical_location(ip): url = 'http://ip.taobao.com/s ...

  2. Xcode 问题

    问题: 昨天在写代码的时候,不知道修改了哪个地方,Xcode6突然犯病了,在当前项目下无法代码提示,但是在新建工程中没有任何问题,其中重装了Xcode6也没有把问题解决, 最终的解决办法是: 在fin ...

  3. Codeforces Round #331 (Div. 2) B. Wilbur and Array

    B. Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. hadoop压缩和解压

    最近有一个hadoop集群上的备份需求.源文件有几百G,如果直接复制太占用磁盘空间.将文件从hadoop集群下载到本地,压缩之后再上传到hadoop则太耗时间.于是想到能否直接在HDFS文件系统上进行 ...

  5. 工作笔记 --->新疆统计分析添加市场管理员相关功能笔记

    先上一张大致需求的图 表信息 点击首页 “管理站点”时打开一个窗口 <a href="javascript:void(0);" onclick="javascrip ...

  6. 最短路之spfa系列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t ...

  7. python3中字典的遍历和合并

    #字典的遍历方式 dic={"a":1,"b":2,"c":3} for k in dic: print (k,dic[k]) for k, ...

  8. ubuntu 玩转 nodejs

    安装nginx 首先添加nginx_signing.key(必须,否则出错) $ wget http://nginx.org/keys/nginx_signing.key $ sudo apt-key ...

  9. 1.0 docker介绍

    简介: 一种虚拟化的方案 将应用程序自动部署到容器   特点: 轻量 环境的一直性 提高开发生命周期 使用面向服务的架构   场景: 开发.测试.部署 创建隔离的运行环境 集群测试环境 云计算应用   ...

  10. 【Python学习】Jupyter解决单个变量输出问题

    使用Jupyter的时候有时候发现,我明明写了好几个变量打印,但是它只显示最后一个.Out只有一个. 但是使用下面的语句.就可以实现多个输出. from IPython.core.interactiv ...