博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3277 Marriage Match III(最大流+二分+并查集)
阅读量:6426 次
发布时间:2019-06-23

本文共 4111 字,大约阅读时间需要 13 分钟。

Marriage Match III

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 771    Accepted Submission(s): 236

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
 

 

Source
 

 

Recommend
chenheng

 

 

 

这题和HDU3081 很类似。

但是因为可以随意选择K个人。

所以要将女孩拆成两个点。

将每个女孩u分为u1,u2,若u喜欢v则加一条u1到v的边 否则加一条u2到v的边,令加u1到u2的容量为k的边;

这个拆点的想法非常巧妙。

//============================================================================// Name        : HDU.cpp// Author      : // Version     :// Copyright   : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include 
#include
#include
#include
using namespace std;const int MAXN=1000;int maze[MAXN][MAXN];int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN];int flow[MAXN][MAXN];int sap(int start,int end,int nodenum){ memset(cur,0,sizeof(cur)); memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); memset(flow,0,sizeof(flow)); int u=pre[start]=start,maxflow=0,aug=-1; gap[0]=nodenum; while(dis[start]
maze[u][v]-flow[u][v])aug=maze[u][v]-flow[u][v]; pre[v]=u; u=cur[u]=v; if(v==end) { maxflow+=aug; for(u=pre[u];v!=start;v=u,u=pre[u]) { flow[u][v]+=aug; flow[v][u]-=aug; } aug=-1; } goto loop; } int mindis=nodenum-1; for(int v=0;v
dis[v]) { cur[u]=v; mindis=dis[v]; } if((--gap[dis[u]])==0)break; gap[dis[u]=mindis+1]++; u=pre[u]; } return maxflow;}int F[260];int find(int x){ if(F[x]==-1)return x; else return F[x]=find(F[x]);}void bing(int x,int y){ int t1=find(x); int t2=find(y); if(t1!=t2)F[t1]=t2;}int n,m,f;int K;int a[250*250],b[250*250];bool check(int t){ for(int i=1;i<=n;i++) maze[0][i]=t; for(int i=2*n+1;i<=3*n;i++) maze[i][3*n+1]=t; if(sap(0,3*n+1,3*n+2)==t*n)return true; else return false;}void solve(){ memset(maze,0,sizeof(maze)); for(int i=0;i

 

转载地址:http://ymyga.baihongyu.com/

你可能感兴趣的文章
【Postgresql】postgresql9.3.9版本部署
查看>>
zip压缩工具 tar打包打包并压缩
查看>>
Linux基础知识之linux相关介绍
查看>>
DHCP自动分配地址;DHCP给指定的客户端分配指定的IP地址;
查看>>
python数据分析库pandas
查看>>
简单介绍linux中的shell脚本
查看>>
Linux中 LVS 的介绍
查看>>
用户、组和权限
查看>>
HTTPD常用配置
查看>>
混合多系统虚拟网卡核间中断实现
查看>>
JDK Unsafe 源码完全注释
查看>>
CodeMix入门基础知识
查看>>
PyCharm入门教程——用户界面导览
查看>>
你真的完全了解Java动态代理吗?看这篇就够了
查看>>
net-ldap for ruby openNebula ldap
查看>>
OpenNebula openldap集成
查看>>
配置VRRP单备份组
查看>>
学习经验
查看>>
有些无线客户端无法通过cisco871路由器DHCP获得ip
查看>>
关于python类的组合
查看>>