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 ...
随机推荐
- swift语言点评二十-扩展
结论:扩展无法修改原来的数据结构. Extensions can add new functionality to a type, but they cannot override existing ...
- git pull 失败 ,提示:fatal: refusing to merge unrelated histories
首次 git pull 时失败,并提示:fatal: refusing to merge unrelated histories 在使用git pull 命令时,添加一个可选项 git pull or ...
- Linux grep 筛选语句
1. 同时满足多个条件 cat logs.log |grep 123|grep 'abc'|more --查询logs.log中同时满足123和abc的句子 2. 满足任意一个条件 cat ...
- 使用js获取url中的get参数并转成json格式
写在前面的 没啥说的 上代码 思路就是先获取到?后面的参数区,然后 利用字符串转数组方法获取到各个参数 var json = {}; var url = 'https://www.baidu.com/ ...
- LaTex的几种数学符号字体以及相关说明
\mathrm is the normal upright Roman font \mathnormal is the normal math italic font: $\mathnormal{a} ...
- NOIp2018模拟赛三十三
神奇的一场... 成绩:100+0+14=114 A题是个体面很恐怖的题...然而看懂题意之后转化一下就变成了一道暴力傻逼题...但是不知道为什么dalao们都没写,讲题的时候挺尴尬的...yrx“瞄 ...
- 【Computer Vision】图像单应性变换/投影/仿射/透视
一.基础概念 1. projective transformation = homography = collineation. 2. 齐次坐标:使用N+1维坐标来表示N维坐标,例如在2D笛卡尔坐标 ...
- Axios 使用时遇到的问题
最近使用 vue 构建一个小项目,在使用 axios 发送 post 请求的时候,发现 axios 发送数据默认使用 json 格式,百度搜了下,更改 ContentType 不管用,最终问题原来是: ...
- C++根据扩展名获取文件图标、类型
简述 在Windows系统中,根据扩展名来区分文件类型,比如:.txt(文本文件)..exe(可执行程序).*.zip(压缩文件),下面,我们来根据扩展名来获取对应的文件图标.类型. 简述 源码 源码 ...
- LeetCode 232: Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...