博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3277 最大流+二分
阅读量:4886 次
发布时间:2019-06-11

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

Marriage Match III

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

Total Submission(s): 2143    Accepted Submission(s): 646

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
 题意:
有n个女生n个男生,每个女生可以和她喜欢的男生配对也可以和她的朋友喜欢的男生配对还可以和不超过k个不喜欢的男生配对,当所有的人都配对了时游戏结束,要求每一轮游戏中互相已经配对的两个人以后就不能再配对了问可以进行多少轮游戏。
输入t组数据
输入n,m,k,f,表示n对人,m个喜欢关系,f个朋友关系
输入m行a b 表示女生a喜欢男生b
输入f行a b 表示女生a和女生b是朋友
代码:
//和上一道题类似,只不过是加了每个女生可以选k个不喜欢的男生这一条件,类似上一题建图,然后把女生拆点容量为k//拆点后的点连向她不喜欢的男生容量为1,然后就一样了。#include
#include
#include
#include
#include
using namespace std;const int maxn=1000;const int inf=0x7fffffff;int mp[maxn][maxn],fat[maxn];int find(int x){ return fat[x]==x?x:fat[x]=find(fat[x]);}void connect(int x,int y){ int xx=find(x),yy=find(y); if(xx!=yy) fat[yy]=xx;}struct Edge{ int from,to,cap,flow; Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}};struct Dinic{ int n,m,s,t; vector
edges; vector
g[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; void init(int n){ this->n=n; for(int i=0;i
q; q.push(s); d[s]=0; vis[s]=1; while(!q.empty()){ int x=q.front();q.pop(); for(int i=0;i<(int)g[x].size();i++){ Edge &e=edges[g[x][i]]; if(!vis[e.to]&&e.cap>e.flow){ vis[e.to]=1; d[e.to]=d[x]+1; q.push(e.to); } } } return vis[t]; } int Dfs(int x,int a){ if(x==t||a==0) return a; int flow=0,f; for(int&i=cur[x];i<(int)g[x].size();i++){ Edge &e=edges[g[x][i]]; if(d[x]+1==d[e.to]&&(f=Dfs(e.to,min(a,e.cap-e.flow)))>0){ e.flow+=f; edges[g[x][i]^1].flow-=f; flow+=f; a-=f; if(a==0) break; } } return flow; } int Maxflow(int s,int t){ this->s=s;this->t=t; int flow=0; while(Bfs()){ memset(cur,0,sizeof(cur)); flow+=Dfs(s,inf); } return flow; }}dc;bool solve(int k,int n,int mid){ dc.init(3*n+2); for(int i=1;i<=n;i++){ dc.Addedge(0,i,mid); dc.Addedge(i,i+n,k); for(int j=2*n+1;j<=3*n;j++){ if(mp[i][j]) dc.Addedge(i,j,1); else dc.Addedge(i+n,j,1); } dc.Addedge(i+2*n,3*n+1,mid); } return n*mid==dc.Maxflow(0,3*n+1);}int main(){ int t,n,m,k,f; scanf("%d",&t); while(t--){ scanf("%d%d%d%d",&n,&m,&k,&f); int a,b; memset(mp,0,sizeof(mp)); for(int i=1;i<=n;i++) fat[i]=i; for(int i=1;i<=m;i++){ scanf("%d%d",&a,&b); mp[a][b+2*n]=1; } for(int i=1;i<=f;i++){ scanf("%d%d",&a,&b); connect(a,b); } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(find(i)==find(j)) for(int k=2*n+1;k<=3*n;k++) mp[i][k]=mp[j][k]=(mp[i][k]||mp[j][k]); } } int l=0,r=n,mid,ans=0; while(l<=r){ mid=(l+r)/2; if(solve(k,n,mid)){ ans=mid; l=mid+1; } else r=mid-1; } printf("%d\n",ans); } return 0;}

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6933913.html

你可能感兴趣的文章
初识web2py
查看>>
script & scriptreplay
查看>>
Docker最全教程——从理论到实战(二)
查看>>
HDU4109-instruction agreement(差分约束-最长路+建立源点,汇点)
查看>>
Promise 练习
查看>>
用户登陆--判断输入密码错误3次后冻结该账号
查看>>
无监督学习:Deep Generative Mode(深度生成模型)
查看>>
搭建本地pip源
查看>>
学习进度条
查看>>
UserControl关闭
查看>>
ASP.NET浏览器定义文件及IE兼容模式
查看>>
第三章程序的机器级表示 学习报告
查看>>
在iOS应用中直接打开系统的“设置”
查看>>
hdu3306:Another kind of Fibonacci
查看>>
BZOJ1777: [Usaco2010 Hol]rocks 石头木头
查看>>
linux nginx 配置php
查看>>
vscode Gitlens插件 查看代码提交
查看>>
常用工具函数封装
查看>>
spring事务回滚异常问题总结
查看>>
leetcode子集和问题
查看>>