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): 1994    Accepted Submission(s): 586


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
题意:有n个女生和n个男生,m个关系,表示某个女生和某个男生关系好,然后再给出女生之间的f个关系,表示某个女生与另一个女生关系好,一个女生可以跟k个关系不好的男生变得关系好
两个女生是朋友的话,跟某个女生关系好的男生也会跟女生的朋友关系好
现在女生每轮都选择不同的男生,问最多可以选择几轮

思路:和上一题差不多,多了一个k,那么只要把每个女生拆成两个点,另一个点往关系不好的男生那连k条边即可

注意用Dinic会超时,这里找了半天的ISAP,终于找到一个很快的模板

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1000
#define INF 99999999
struct Edge
{
    int v,next,cap;
} edge[N*N*2];
struct E
{
    int u,v,cap;
} e[N*N];
int pre[N];
int cnt,head[N],gap[N],dis[N],start,End,ans;
int vis[N][N];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap)
{
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cap=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int finds(int x)
{
    int r=x;
    while(r!=pre[r])
        r=pre[r];
    while(x!=pre[x])
    {
        int l=x;
        x=pre[x];
        pre[l]=r;
    }
    return r;
}
void mix(int a,int b)
{
    int x=finds(a),y=finds(b);
    if(x!=y) pre[x]=y;
}
int dfs(int u,int minflow)
{
    if(u==End)return minflow;
    int i,v,f,min_dis=ans-1,flow=0;
    for(i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].v;
        if(edge[i].cap<=0)continue;
        if(dis[v]+1==dis[u])
        {
            f=dfs(v,edge[i].cap>minflow-flow?minflow-flow:edge[i].cap);
            edge[i].cap-=f;
            edge[i^1].cap+=f;
            flow+=f;
            if(flow==minflow)break;
            if(dis[start]>=ans)return flow;
        }
        min_dis=min_dis>dis[v]?dis[v]:min_dis;
    }
    if(flow==0)
    {
        if(--gap[dis[u]]==0)
            dis[start]=ans;
        dis[u]=min_dis+1;
        gap[dis[u]]++;
    }
    return flow;
}
int isap()///ans等于End+1
{
    int maxflow=0;
    memset(gap,0,sizeof(gap));
    memset(dis,0,sizeof(dis));
    gap[0]=ans;
    while(dis[start]<ans)
        maxflow+=dfs(start,INF);
    return maxflow;
}
int main()
{
    int T,n,m,k,f;
    int u,v;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d %d",&n,&m,&k,&f);
        for(int i=1; i<=n; i++)
            pre[i]=i;
        for(int i=1; i<=m; i++)
            scanf("%d %d",&e[i].u,&e[i].v);
        for(int i=1; i<=f; i++)
        {
            scanf("%d %d",&u,&v);
            mix(u,v);
        }
        for(int i=1; i<=n; i++)
            pre[i]=finds(i);
        start=0,End=3*n+1,ans=End+1;
        int l=0,r=n,ans;
        int tt=cnt;
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=m; i++)
        {
            int x=pre[e[i].u];
            vis[x][e[i].v]=1;
        }
        while(l<=r)
        {
            init();
            int mid=(l+r)>>1;
            for(int i=1; i<=n; i++)
                addedge(start,i,mid);
            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                    if(vis[pre[i]][j])
                        addedge(i,2*n+j,1);
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=n; j++)
                    if(!vis[pre[i]][j])
                        addedge(n+i,2*n+j,1);
                addedge(i,n+i,k);
            }
            for(int i=2*n+1; i<=3*n; i++)
                addedge(i,End,mid);
            if(isap()==mid*n) ans=mid,l=mid+1;
            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、付费专栏及课程。

余额充值