HDU 3277 Marriage Match III
Marriage Match III
This problem will be judged on HDU. Original ID: 3277
64-bit integer IO format: %I64d Java class name: Main
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
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
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的更多相关文章
- HDU 3277 Marriage Match III(二分+最大流)
HDU 3277 Marriage Match III 题目链接 题意:n个女孩n个男孩,每一个女孩能够和一些男孩配对,此外还能够和k个随意的男孩配对.然后有些女孩是朋友,满足这个朋友圈里面的人.假设 ...
- HDU 3277 Marriage Match III(并查集+二分答案+最大流SAP)拆点,经典
Marriage Match III Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 【HDOJ】3277 Marriage Match III
Dinic不同实现的效率果然不同啊. /* 3277 */ #include <iostream> #include <string> #include <map> ...
- HDU 3081 Marriage Match II(二分法+最大流量)
HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
- hdu 3416 Marriage Match IV (最短路+最大流)
hdu 3416 Marriage Match IV Description Do not sincere non-interference. Like that show, now starvae ...
- HDU 3416 Marriage Match IV (求最短路的条数,最大流)
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...
随机推荐
- (转载) Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框
Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框 标签: Android清除功能EditText仿IOS的输入框 2013-09-04 17:33 70865人阅读 ...
- hiho 1571 - 贪心好题*
题目链接 小Hi在帮助钢铁侠开发新的盔甲.这套新盔甲一共包含M种武器插槽,其中第i种插槽有Ci个.每个插槽最多安装一个武器模块. 小Hi一共准备了N个武器模块,编号1~N.每个武器模块都有三个参数Vi ...
- [Java] Protect, Private and Public的区别
Java中的private.protected.public和default的区别 (2014-03-21 22:29:14) 转载▼ 标签: java java修饰符 it (1)对于publi ...
- swift语言点评八-枚举
总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a “raw” valu ...
- jq滚动条美化
https://github.com/inuyaksa/jquery.nicescroll(插件地址) https://blog.csdn.net/zyy_0725/article/details/8 ...
- VB学习生成JavaBean
Application.ActiveWorkbook.Path 获取当前excel文件所在的文件地址 Excel VBA中表示当前工作簿,有Activeworkbook和Thisworkbook 两种 ...
- spring boot ---web应用开发-错误处理
一.错误的处理 方法一:Spring Boot 将所有的错误默认映射到/error, 实现ErrorController @Controller @RequestMapping(value = &qu ...
- 《Spring技术内幕》笔记-Spring的设计理念和总体架构
1.Spring的主要子项目: -1.Spring Framework(Core):Spring项目的核心.提供IoC,AOP,MVC等核心功能. -2.Spring Web Flow ...
- Thinking in States
Thinking in States Niclas Nilsson PEOPLE IN THE REAL WORLD HAVE A WEIRD RELATIONSHIP WITH STATE. Thi ...
- iOS gzip解压
1. 导入libz库(如:libz 1.2.5.dylib) 2. 引入头文件 #import "zlib.h" 3. 实现解压(输入输出都为NSData对象) -(NSData ...