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. [辅助软件] 微信小程序开发资源汇总 接入指南

    https://github.com/justjavac/awesome-wechat-weapp https://github.com/justjavac/awesome-wechat-weapp ...

  2. PowerDesigner 技巧【1】

    Name与Code同步的问题: PowerDesigner中,修改了某个字段的name,其code也跟着修改,这个问题很讨厌,因为一般来说,name是中文的,code是字段名. 解决方法如下: 1.选 ...

  3. HDU4625:Strongly connected(思维+强连通分量)

    Strongly connected Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. derby数据库windows自带的客户端

    本示例演示用windows自带的ij来操作derby数据库,包括建库,建表,插入数据,查询数据 首先要配置环境变量: 其次打开cmd输入如下图所示的命令: java代码如下: package com. ...

  5. AIDL 简单实现

    实现步骤1.建立一个aidl文件,在里面定义好接口,注意里面不能写public修饰符,同接口一样,包名要一致. package com.systemset.aidl; interface ILight ...

  6. 自定义函数,根据p个数,自适应剧中效果

    //最后投保进程line-height 自适应居中; function self_adaption(){ $('.last_place').each(function(){ var _this = $ ...

  7. ubuntu18.04server设置静态IP

    16.04以后的版本配置静态IP是类似这样的文件 /etc/netplan/50-cloud-init.yaml 1.查询网卡名称 2.修改配置文件/etc/netplan/50-cloud-init ...

  8. python基础---输入输出

    1.输入字符串. name=input()   or name=input('please input a string') 这样可以接收一个字符串,包括空格,都可以输入.只有回车不接受,作为结束符, ...

  9. 图论&数学:矩阵树定理

    运用矩阵树定理进行生成树计数 给定一个n个点m条边的无向图,问生成树有多少种可能 直接套用矩阵树定理计算即可 矩阵树定理的描述如下: 首先读入无向图的邻接矩阵,u-v G[u][v]++ G[v][u ...

  10. 原生js addclass,hasClass,removeClass,toggleClass的兼容

    (function (window) { 'use strict'; // class helper functions from bonzo https://github.com/ded/bonzo ...