hdu3081 Marriage Match II(最大流+二分+并查集)

Marriage Match II

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


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个男孩,m条边,表示女孩u和男孩v是朋友,k个关系,表示女孩u和女孩v是朋友,当女孩u和v是朋友时,
那么u的男朋友都是v的男朋友,v的男朋友也都是u的男朋友,现在每个女孩要选他的一个男朋友结婚,问有多少种结婚方案

观察题目,发现这是一个二部图,而对于二部图有多少种完备匹配方案,可以用二分枚举limit,然后网络流建边源点与左边点流量为limit,右边点与汇点流量为limit,二部图左边和右边点建边为1,判断 最大流/n==limit

首先我们可以先用并查集+floyd 将每个女孩 的朋友 的男朋友关系传递给她
然后二分limit
1.女孩到男孩建边,容量为1
2.源点到女孩建边,容量为limit
3.男孩到汇点建边,容量为limit
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=2005+50;
const int maxm=1e7+50;
struct node
{
    node() {};
    node(int tv,int tw,int tnext)
    {
        v=tv,w=tw,next=tnext;
    };
    int v,w,next;
} e[maxm];
int first[maxn],vis[maxn],dis[maxn],tot;
void add_edge(int u,int v,int w)
{
    e[tot]=node(v,w,first[u]);
    first[u]=tot++;
    e[tot]=node(u,0,first[v]);
    first[v]=tot++;
}
int bfs(int s,int t)
{
    mem(vis,0);
    mem(dis,0);
    queue<int>q;
    q.push(s);
    vis[s]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=first[u]; ~i; i=e[i].next)
        {
            if(!vis[e[i].v]&&e[i].w>0)
            {
                vis[e[i].v]=1;
                dis[e[i].v]=dis[u]+1;
                q.push(e[i].v);
            }
        }
    }
    return dis[t];
}
int dfs(int u,int t,int flow)
{
    if(u==t)return flow;
    for(int i=first[u]; ~i; i=e[i].next)
    {
        if(dis[e[i].v]==dis[u]+1&&e[i].w>0)
        {
            int dd=dfs(e[i].v,t,min(e[i].w,flow));
            if(dd)
            {
                e[i].w-=dd;
                e[i^1].w+=dd;
                return dd;
            }
        }
    }
    dis[u]=0;
    return 0;
}
int Dinic(int s,int t)
{
    int ans=0,flow;
    while(bfs(s,t))
    {
        while(flow=dfs(s,t,INF))
            ans+=flow;
    }
    return ans;
}
void init()
{
    mem(first,-1);
    tot=0;
}
int f[maxn];
int getf(int x)
{
    if(f[x]==x)return x;
    return f[x]=getf(f[x]);
}
void mix(int x,int y)
{
    int tx=getf(x);
    int ty=getf(y);
    if(tx!=ty)
        f[tx]=ty;
}

int n,x,y;
bool flag[maxn][maxn];
void floyd()
{
    for(int i=1; i<=n; i++)
    {
        int x=getf(i);
        for(int j=1; j<=n; j++)
        {
            if(!flag[i][j]&&flag[x][j])
                flag[i][j]=1;
        }
    }
}
bool solve(int limit)
{
    init();
    for(int i=1; i<=n; i++)
        add_edge(0,i,limit);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            if(flag[i][j])
                add_edge(i,n+j,1);
        }
    for(int i=1; i<=n; i++)
        add_edge(n+i,2*n+1,limit);
    int now=Dinic(0,2*n+1);
    return now==limit*n;
}
int main()
{
    int t,m,k;
    scanf("%d",&t);
    while(t--)
    {
        mem(flag,0);
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1; i<=n; i++)
            f[i]=i;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&x,&y);
            flag[x][y]=1;
        }
        for(int i=0; i<k; i++)
        {
            scanf("%d%d",&x,&y);
            mix(x,y);
        }
        for(int i=1;i<=n;i++)
        {
            int x=getf(i);
            for(int j=1;j<=n;j++)
            {
                if(flag[i][j]&&!flag[x][j])
                    flag[x][j]=1;
            }
        }
        floyd();
        int l=0,r=n,ans;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(solve(mid))
            {
                l=mid+1;
                ans=mid;
            }
            else
                r=mid-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值