hdu 3277 Marriage Match III【最大流+并查集+二分枚举】

Marriage Match III

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1491    Accepted Submission(s): 440


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
这道题和hdu 3081差不多,只不过在原题意上多了一条(女孩可一与k个任意男孩(也许吵架)相连),因此解决方法也可以在原基础上加工。
推荐一篇hdu 3081的博客,链接:http://blog.csdn.net/qq564690377/article/details/7857983
分析:将女孩拆点(v1,v2)用并查集将女孩之间的关系连接起来,再在此基础上将v1与所有关系(直接和间接)的男孩连接,
v2与所有没关系的男孩连接,v1,v2之间的权值为k,源点与所有v1连接,汇点与所有男孩连接权值为mid(mid为0到n之间的数),然后,
二分枚举mid(0~n)跑最大流,找出是满流(即最大流为n*mid)的最大mid值,此mid值即为答案。
代码示例:
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<queue> #define Min(a,b) a<b?a:b #define inf 1000000000 #define maxn 200000 #define maxh 1000 using namespace std; typedef struct //前向星  {     int to,next,w; }node; typedef struct {     int x,t; }dep; node E[maxn]; int head[maxh],headx[maxh],deep[maxh],cnt; int map[maxh][maxh]; int set[maxh]; int s,t,k; /zuidaliu/// void init() {     memset(head,-1,sizeof(head));     cnt=0; } void add(int a,int b,int c) {     E[cnt].to=b,E[cnt].w=c,E[cnt].next=head[a],head[a]=cnt++;     E[cnt].to=a,E[cnt].w=0,E[cnt].next=head[b],head[b]=cnt++; } int bfs(int s,int t,int n) {     memset(deep,255,sizeof(deep));     dep fir,nex;     queue<dep>Q;     fir.x=s,fir.t=0,deep[s]=0;     Q.push(fir);     while(!Q.empty())     {         fir=Q.front();         Q.pop();         for(int i=head[fir.x];i+1;i=E[i].next)         {             nex.x=E[i].to;             nex.t=fir.t+1;             if(deep[nex.x]!=-1||!E[i].w)             continue;             deep[nex.x]=nex.t;             Q.push(nex);         }     }     for(int i=0;i<=n;i++)     headx[i]=head[i];     return deep[t]!=-1; } int dfs(int s,int t,int flow) {     if(s==t)     return flow;     int newflow=0;     for(int i=headx[s];i+1;i=E[i].next)     {            headx[s]=i;         int to=E[i].to;         int w=E[i].w;         if(!w||deep[to]!=deep[s]+1)         continue;         int temp=dfs(to,t,min(w,flow-newflow));         newflow+=temp;         E[i].w-=temp;         E[i^1].w+=temp;         if(newflow==flow)         break;     }     if(!newflow)deep[s]=0;     return newflow; } int Dinic(int s,int t,int n) {     int sum=0;     while(bfs(s,t,n))     {         sum+=dfs(s,t,inf);     }     return sum; } /bingcharji/// int findx(int x) {     if(x!=set[x])     set[x]=findx(set[x]);     return set[x]; } void fun(int x,int y) {     x=findx(x);     y=findx(y);     if(x!=y)     set[x]=y; } /jiantu/// void built(int mid,int n) {        int n1=n,n2=2*n;     init(); //出始前向星      for(int i=1;i<=n;i++)     {         add(s,i,mid);         add(i+n2,t,mid);         add(i,i+n1,k);     }     for(int i=1;i<=n;i++)     for(int j=1;j<=n;j++)     {         if(map[i][j])         {             add(i,j+n2,1);         }         else         {             add(i+n1,j+n2,1);         }     } } zhuhanshu// int main() {     int T,n,m,f;     int a,b,xi,yi;     int l,r,mid;     scanf("%d",&T);     while(T--)     {         scanf("%d%d%d%d",&n,&m,&k,&f);         for(int i=0;i<=n;i++)         {             set[i]=i;             for(int j=0;j<=n;j++) //初始化              {                 map[i][j]=0;             }         }         for(int i=0;i<m;i++)         {             scanf("%d%d",&a,&b);             map[a][b]=1;         }         for(int i=0;i<f;i++)         {             scanf("%d%d",&a,&b);             fun(a,b);         }         for(int i=1;i<=n;i++)         {             xi=findx(i);             for(int j=1;j<=n;j++)             {                 yi=findx(j);                 if(xi==yi&&i!=j)                 {                     for(int l=1;l<=n;l++)                     {                         if(map[j][l])                         {                             map[i][l]=1;                         }                     }                 }             }         }         l=0,r=n+1;         s=3*n+1,t=3*n+2;         while(l!=r-1)         {             mid=(l+r)/2;             built(mid,n);             int sum=Dinic(s,t,t);             if(sum==n*mid)             {                 l=mid;             }             else             {                 r=mid;             }         //    printf("sum=%d\n",sum);         }         printf("%d\n",l);     }     return 0; }                                           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值