Marriage Match III

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

Total Submission(s): 1581    Accepted Submission(s): 464

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
题意:共同拥有2*n个人。一半女一半男,女与男有m个关系。表示能够成为一对,接下来 f 对女的与女的  的朋友关系,假设a与b是朋友。那么表示a女与b女的相连男性也能够成为一对,相同b也与a的相连男性可成为一对,女的之间的朋友关系能够传递。且每一个女性能够再随意选择K人。

一组配对情况为全部的女性都有一个与之配对的男性(一对一的关系)。假设还有其它组配对情况,那么全部的女性配对不能够再与原来的男性配成对。问最多有多少组配对情况。


 这题和HDU3081 非常类似。



可是由于能够任意选择K个人。



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



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



这个拆点的想法很巧妙。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define captype int const int MAXN = 100010; //点的总数
const int MAXM = 4000100; //边的总数
const int INF = 1<<30;
struct EDG{
int to,next;
captype cap,flow;
}edg[MAXM];
int eid,head[MAXN];
int gap[MAXN];
int dis[MAXN];
int cur[MAXN];
int pre[MAXN]; void init(){
eid=0;
memset(head,-1,sizeof(head));
}
void addEdg(int u,int v,captype c,captype rc=0){
edg[eid].to=v; edg[eid].next=head[u];
edg[eid].cap=c; edg[eid].flow=0; head[u]=eid++; edg[eid].to=u; edg[eid].next=head[v];
edg[eid].cap=rc; edg[eid].flow=0; head[v]=eid++;
}
captype maxFlow_sap(int s,int t,int n){
memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
memcpy(cur,head,sizeof(head));
pre[s]=-1;
gap[0]=n; captype ans=0;
int u=s;
while(dis[s]<n){
if(u==t){
captype mint=INF;
int id;
for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to])
if(mint>edg[i].cap-edg[i].flow){
mint=edg[i].cap-edg[i].flow;
id=i;
}
for(int i=pre[u]; i!=-1; i=pre[edg[i^1].to]){
edg[i].flow+=mint;
edg[i^1].flow-=mint;
}
ans+=mint;
u=edg[id^1].to;
continue;
}
bool flag=0;
for(int i=cur[u]; i!=-1; i=edg[i].next)
if(edg[i].cap>edg[i].flow&&dis[u]==dis[edg[i].to]+1){
cur[u]=pre[edg[i].to]=i;
flag=true;
break;
}
if(flag){
u=edg[cur[u]].to;
continue;
}
int minh=n;
for(int i=head[u]; i!=-1; i=edg[i].next)
if(edg[i].cap>edg[i].flow && minh>dis[edg[i].to]){
cur[u]=i; minh=dis[edg[i].to];
}
gap[dis[u]]--;
if(!gap[dis[u]]) return ans;
dis[u]=minh+1;
gap[dis[u]]++;
if(u!=s)
u=edg[pre[u]^1].to;
}
return ans;
} int fath[MAXN];
int findroot(int x){
if(x!=fath[x])
fath[x]=findroot(fath[x]);
return fath[x];
}
void setroot(int x,int y){
x=findroot(x);
y=findroot(y);
fath[x]=y;
}
void rebuildMap(int mapt[255][255],int n){//处理朋友之间的关系
int mp[255][255]={0};
for(int i=1; i<=n; i++)
fath[i]=findroot(i);
for(int i=1; i<=n; i++){
int j=fath[i];
for(int e=1; e<=n; e++)
mp[j][e]|=mapt[i][e];
}
for(int i=1; i<=n; i++){
int j=fath[i];
for(int e=1; e<=n; e++)
mapt[i][e]=mp[j][e];
}
}
int main()
{
int T,n,m,k,f,mapt[255][255];
int u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d",&n,&m,&k,&f); init();
memset(mapt,0,sizeof(mapt));
for(int i=1; i<=n; i++)
fath[i]=i; while(m--){
scanf("%d%d",&u,&v);
mapt[u][v]=1;
}
while(f--){
scanf("%d%d",&u,&v);
setroot(u,v);
}
rebuildMap(mapt,n); int s=0, t=3*n+1;
for(int i=1; i<=n; i++){
addEdg(s,i,0);
addEdg(i,i+n,k);
for(int j=1; j<=n; j++)
if(mapt[i][j])
addEdg(i,j+2*n,1);
else
addEdg(i+n,j+2*n,1); addEdg(i+2*n,t,0);
} int ans=0 , l=0 , r=n ,mid;
while(l<=r){
mid=(l+r)>>1; for(int i=0; i<eid; i++)
edg[i].flow=0;
for(int i=head[s]; i!=-1; i=edg[i].next)
edg[i].cap=mid;
for(int i=head[t]; i!=-1; i=edg[i].next)
edg[i^1].cap=mid; if(n*mid==maxFlow_sap(s,t,t+1))
ans=mid,l=mid+1;
else
r=mid-1;
} printf("%d\n",ans);
}
}

最新文章

  1. 剑指Offer面试题:31.两个链表的第一个公共节点
  2. Linux selinux iptables
  3. 《C与指针》第十五章练习
  4. 为什么内联函数,构造函数,静态成员函数不能为virtual函数
  5. Java获取本地IP地址
  6. 没有Path的Binding
  7. android点击状态分析
  8. 图解VS2010打包全过程
  9. Prefix the choice with ! to persist it to bower.json ? Answer (问你选择哪个1,2,3.........)
  10. word和.txt文件转html 及pdf文件, 使用poi jsoup itext心得
  11. Linux操作系统-命令-vmstat
  12. beego学习1
  13. 联想R720面板右下部分按压后和上面按键串联了
  14. 03-python-装饰器
  15. CSS 变量教程
  16. SVC(STM32)
  17. 20155331 2016-2017-2 《Java程序设计》第6周学习总结
  18. 改善C#程序的建议9:使用Task代替ThreadPool和Thread
  19. for 续4
  20. js小记:对象、原型及原型链、面向对象编程

热门文章

  1. [NOI2012]随机数生成器
  2. Python int 中 add abs 方法
  3. Python divmod方法
  4. 【Java】 剑指offer(7) 二叉树的下一个结点
  5. 用户设置与virtual host配置
  6. Python3 - 基础知识、基本了解
  7. 启用mysql的sql日志
  8. 基于tensorflow搭建一个神经网络
  9. 4558: [JLoi2016]方
  10. java运行时could not open ........jvm.cfg问题的解决