Marriage Match III

Time Limit: 4000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3277
64-bit integer IO format: %I64d      Java class name: Main

 
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

Source

 
解题:最大流。。。
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = ){
to = x;
flow = y;
next = z;
}
}e[];
int head[maxn*maxn],d[maxn*maxn],cur[maxn*maxn];
int tot,n,m,k,S,T,uf[maxn];
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
int Find(int x){
if(x != uf[x]) uf[x] = Find(uf[x]);
return uf[x];
}
bool bfs(){
queue<int>q;
q.push(T);
memset(d,-,sizeof d);
d[T] = ;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i^].flow > && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[S] > -;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow > && d[e[i].to]+== d[u]&&(a=dfs(e[i].to,min(e[i].flow,low)))){
e[i].flow -= a;
low -= a;
e[i^].flow += a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(){
int ret = ;
while(bfs()){
memcpy(cur,head,sizeof head);
ret += dfs(S,INT_MAX);
}
return ret;
}
bool con[maxn][maxn];
int g[maxn*maxn],b[maxn*maxn];
void build(int mid){
memset(head,-,sizeof head);
tot = ;
for(int i = ; i <= n; ++i){
add(i,i+n,k);
add(S,i,mid);
add(i+*n,T,mid);
}
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
if(con[Find(i)][j]) add(i,j+*n,);
else add(i+n,j+*n,);
}
int main(){
int kase,f,u,v;
scanf("%d",&kase);
while(kase--){
scanf("%d%d%d%d",&n,&m,&k,&f);
S = ;
T = *n + ;
for(int i = ; i < maxn; ++i) uf[i] = i;
for(int i = ; i < m; ++i)
scanf("%d%d",g+i,b+i);
for(int i = ; i < f; ++i){
scanf("%d%d",&u,&v);
u = Find(u);
v = Find(v);
if(u != v) uf[u] = v;
}
memset(con,false,sizeof con);
for(int i = ; i < m; ++i)
con[Find(g[i])][b[i]] = true;
int low = ,high = n,ret = ;
while(low <= high){
int mid = (low + high)>>;
build(mid);
if(dinic() == n*mid){
ret = mid;
low = mid+;
}else high = mid - ;
}
printf("%d\n",ret);
}
return ;
}

HDU 3277 Marriage Match III的更多相关文章

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

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

  2. HDU 3277 Marriage Match III(并查集+二分答案+最大流SAP)拆点,经典

    Marriage Match III Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. 【HDOJ】3277 Marriage Match III

    Dinic不同实现的效率果然不同啊. /* 3277 */ #include <iostream> #include <string> #include <map> ...

  4. HDU 3081 Marriage Match II(二分法+最大流量)

    HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...

  5. HDU 3416 Marriage Match IV (最短路径,网络流,最大流)

    HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...

  6. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  7. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

  8. hdu 3416 Marriage Match IV (最短路+最大流)

    hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...

  9. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

随机推荐

  1. RocketMQ学习笔记(3)----RocketMQ物理结构和逻辑部署结构

    1. RocketMQ的物理结构 RecketMQ网络部署的特点: Name Server是一个几乎无状态特点,可集群部署,节点之间无任何信息同步的(相对于zookeeper是较为轻量级的). Bro ...

  2. [arc082e]ConvexScore

    题意: 给出直角坐标系中的$N$个点$(X_i,Y_i)$,定义由其中部分点构成的点集为“凸点集”当且仅当这些点恰好能构成一个凸多边形(内部没有其他点). 如图,点集$\{A,C,E\}$和$\{B, ...

  3. 添加ArcGIS数据

    加载arcgis server的rest服务瓦片数据:ol.layer.Tile+ol.source.TileArcGISRest 加载arcgis online的在线瓦片数据:ol.layer.Ti ...

  4. HDU-5009 Paint Pearls 动态规划 双向链表

    题目链接:https://cn.vjudge.net/problem/HDU-5009 题意 给一串序列,可以任意分割多次序列,每次分割的代价是被分割区间中的数字种数. 求分割区间的最小代价.n< ...

  5. luogu P1869 愚蠢的组合数(质因数+瞎搞)

    题意 n<=105 题解 一个数是不是偶数就是看有没有二这个质因子. 所以我们先预处理每个数的阶乘的二这个质因子的数量 然后按公式判断就行了. #include<iostream> ...

  6. linux 安装常用库

    在CentOS安装软件的时候,可能缺少一部分支持库,而报错.这里首先安装系统常用的支持库.那么在安装的时候就会减少很多的错误的出现. [root@bogon 桌面]#  yum install -y ...

  7. 题解 P3369 【【模板】普通平衡树】

    在网上某篇神奇的教程和@codesonic 大佬的标程帮助下,我又肝完了Leafy Tree,跑过来写篇题解(好像以前写过一篇?) 什么是Leafy Tree? Leafy Tree由两种节点组成:辅 ...

  8. Java 学习(11): 面向对象编程—继承(super,this)

    Java 继承 what: 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为.子类从它的父类中继承可访问的数据域和方法,也 ...

  9. Thunderbird导出邮件为PDF文件

    一般如果在windows下,可以通过打印的方式,生成一个PDF文件,可以完成此任务. 而今天在linux下采取此方式,生成的PDF文件居然是空白.原因未知. 而Thunderbird并未提供该功能,这 ...

  10. 利用Matlab自带的深度学习工具进行车辆区域检测与车型识别【Github更新!!!】(三)

    前言 对前面的东西更新了一下.地方包括: 1.GUI的更新,更友好的用户界面 2.支持用手直接画车辆区域,并且识别出来 3.将proposal.detect.fine-grained classifi ...