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. HDU 1548 A strange lift【BFS】

    题意:给出一个电梯,给出它的层数f,给出起点s,终点g,以及在每一层能够上或者下w[i]层,问至少需要按多少次按钮到达终点. 和POJ catch that cow一样,直接用了那一题的代码,发现一直 ...

  2. eclipse的maven工程视图切换

    上面图切换成下面图: 点击eclipse右上角,如下图红圈,然后在选择javaEE这样就切换成javaEE视图了

  3. 将对象a的属性赋值给对象b

    BeanUtils.copyProperties(a,b); 将a的属性赋值给b(ab的共同属性)

  4. NOIp2018模拟赛三十二

    继续挂成傻逼 成绩:100+0+10(90)=110 A全场一眼题,C没取模挂八十分,然后没特判特殊情况又挂十分 A:[agc009b]tournament(太简单,咕了) B:[ATC2142]Bu ...

  5. ES6学习之环境配置

    环境配置 一.建立工程目录 新建dist文件夹(用于存放转化的es5文件).新建src文件夹(用于存放es6文件),在该文件夹下建立index.js文件 二.编写index.html 在根目录下新建i ...

  6. UE4自学随笔(一)

    本文及后续均为个人学习记录所用,难免毫无章法零零碎碎,希望看到此文的诸君勿怪. 一.Actor与Pawn Actor类 在UE4中,Actor类是可以放到游戏场景中的游戏对象的基本类型.你如果想放置任 ...

  7. python做的 QQ未读消息图像

    #!/usr/bin/pythonfrom PIL import Image ,ImageDraw, ImageFont#打开所在的文件im=Image.open('test.jpg')#获取图片对象 ...

  8. Java基础学习总结(1)——equals方法

    一.equals方法介绍 1.1.通过下面的例子掌握equals的用法 package cn.galc.test; public class TestEquals { public static vo ...

  9. ASP.NET-缓存outputcache参数

    给Index加一个60秒的缓存,应该缓存在IIS服务器里面(我猜的) 只对变化的参数page不进行缓存,其他参数返回相同的内容 根据接受的语言的不同不进行缓存 设定缓存的位置 依赖于数据库变化的缓存 ...

  10. Qt之图形(组合)

    简述 使用QPainter绘制图形或者图像时,在重叠区域使用组合模式(Composition_mode).在绘图设备上通过组合模式使用QImage时,必须使用Format_ARGB32_Premult ...