hdu 3081 二分+最大流

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1435    Accepted Submission(s): 512


Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing 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. 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.
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 a integer T, means the number of test cases.
Each test case starts with three integer n, m and f in a line (3<=n<=100,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 2 1 1 2 3 3 2 4 2 4 4 1 4 2 3
 

Sample Output
  
  
2

题意:已知n个男孩,n个女孩,已知女孩可以匹配的男孩,已知女孩之间的朋友关系,玩配对游戏,每一个女孩每一次都必须与不同的男孩配对,问最多可以玩几轮游戏。

解题思路:将女孩与可以匹配的男孩之间建流量为1的边,二分轮数,源点与女孩连边,男孩与汇点连边,然后跑最大流。

代码:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014/1/18 0:58:25
File Name :E.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=10000;
int head[1000],tol,dep[1000],n;
struct node
{
       int next,to,from,cap;
}edge[30000];
void add(int u,int v,int cap)
{
       edge[tol].from=u;
       edge[tol].to=v;
       edge[tol].cap=cap;
       edge[tol].next=head[u];
       head[u]=tol++;

       edge[tol].from=v;
       edge[tol].to=u;
       edge[tol].cap=0;
       edge[tol].next=head[v];
       head[v]=tol++;
}
int bfs(int s,int t)
{
       int que[maxn],front=0,rear=0;
       memset(dep,-1,sizeof(dep));
       dep[s]=0;que[rear++]=s;
       while(front!=rear)
       {
              int u=que[front++];front%=maxn;
              for(int i=head[u];i!=-1;i=edge[i].next)
              {
                     int v=edge[i].to;
                     if(edge[i].cap>0&&dep[v]==-1)
                     {
                            dep[v]=dep[u]+1;
                            que[rear++]=v;
                            rear%=maxn;
                            if(v==t)return 1;
                     }
              }
       }
       return 0;
}
int dinic(int s,int t)
{
       int i,res=0,top;
       int Stack[maxn],cur[maxn];
       while(bfs(s,t))
       {
              memcpy(cur,head,sizeof(head));
              int u=s;top=0;
              while(1)
              {
                     if(u==t)
                     {
                            int min=inf,loc;
                            for(int i=0;i<top;i++)
                            if(min>edge[Stack[i]].cap)
                            {
                                   min=edge[Stack[i]].cap;
                                   loc=i;
                            }
                            for(int i=0;i<top;i++)
                            {
                                   edge[Stack[i]].cap-=min;
                                   edge[Stack[i]^1].cap+=min;
                            }
                            res+=min;
                            top=loc;
                            u=edge[Stack[top]].from;
                     }
                     for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
                     if(edge[i].cap&&dep[u]+1==dep[edge[i].to])break;
                     if(cur[u]!=-1)
                     {
                            Stack[top++]=cur[u];
                            u=edge[cur[u]].to;
                     }
                     else
                     {
                            if(top==0)break;
                            dep[u]=-1;
                            u=edge[Stack[--top]].from;
                     }
              }
       }
       return res;
}
int fa[1000];
int find(int x)
{
      if(fa[x]!=x)
	     fa[x]=find(fa[x]);
      return fa[x];
}
int vis[300][333];
bool judge(int mid)
{
      memset(head,-1,sizeof(head));tol=0;
      for(int i=1;i<=n;i++)
      {
	     add(0,i,mid);
	     add(i+n,2*n+1,mid);
      }
      for(int i=1;i<=n;i++)
	     for(int j=1;j<=n;j++)
	     {
		    if(vis[i][j])
			   add(i,j+n,1);
	     }
      if(dinic(0,2*n+1)==mid*n)
	     return 1;
      return 0;
}
int main()
{
      //freopen("data.in","r",stdin);
      //freopen("data.out","w",stdout);
      int T,i,j,k,m,f;
      scanf("%d",&T);
      while(T--)
      {
	    scanf("%d%d%d",&n,&m,&f);
	    memset(head,-1,sizeof(head));tol=0;
	    memset(vis,0,sizeof(vis));
	    vector<int> ss[500];
	    while(m--)
	    {
		   scanf("%d%d",&i,&j);
		   vis[i][j]=1;
		   ss[i].push_back(j);
	    }
           for(i=1;i<=n;i++)fa[i]=i;
	    while(f--)
	    {
		   scanf("%d%d",&i,&j);
		   int fx=find(i);
		   int fy=find(j);
		   if(fx==fy)continue;
		   fa[fx]=fy;
	    }
	    for(i=1;i<=n;i++)
           {
		   for(j=1;j<=n;j++)
		   {
			  if(find(i)!=find(j))continue;
			  int cnt=ss[i].size();
			  for(k=0;k<cnt;k++)
				 vis[j][ss[i][k]]=1;
			  cnt=ss[j].size();
			  for(k=0;k<cnt;k++)
				 vis[i][ss[j][k]]=1;
		   }
	    }
	    int left=0,right=n,mid;
	    while(left<right)
	    {
		   mid=(left+right+1)>>1;
		   if(judge(mid))left=mid;
		   else right=mid-1;
	    }
	    printf("%d\n",left);
      }
      return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值