hdu 3081 Marriage Match II(二分, 并查集,最大流)

Marriage Match II

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


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
 

Author
starvae
 

Source
 
题意:有n个女生和n个男生,m个关系,表示某个女生和某个男生关系好,然后再给出女生之间的k个关系,表示某个女生与另一个女生关系好

两个女生是朋友的话,跟某个女生关系好的男生也会跟女生的朋友关系好

现在女生每轮都选择不同的男生,问最多可以选择几轮

思路:用并查集处理关系,然后二分答案(每次都要重新建图,Dinic过程中边的容量会被改掉),只要满流即满足条件

注意加边的时候要标记下去重,不然可能会TLE

代码:

#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];
struct E
{
    int u,v,cap;
} e[N*15];
int pre[N],head[N],cnt,num[N],d[N];
int vis[110][110];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
}
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 bfs(int s,int t)
{
    memset(d,-1,sizeof(d));
    d[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v,cap=edge[i].cap;
            if(d[v]==-1&&cap>0)
            {
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
    if(s==t||f==0) return f;
    int flow=0;
    for(int i=head[s]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v,cap=edge[i].cap;
        if(d[v]==d[s]+1&&cap>0)
        {
            int x=min(f-flow,cap);
            x=dfs(v,t,x);
            flow+=x;
            edge[i].cap-=x;
            edge[i^1].cap+=x;
        }
    }
    if(!flow) d[s]=-2;
    return flow;
}
int Dinic(int s,int t)
{
    int flow=0,f;
    while(bfs(s,t))
    {
        while(f=dfs(s,t,INF))
            flow+=f;
    }
    return flow;
}
int main()
{
    int T,n,m,f;
    int u,v;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d",&n,&m,&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);
        int s=0,t=2*n+1;
        int l=0,r=n,ans;
        int tt=cnt;
        while(l<=r)
        {
            init();
            int mid=(l+r)>>1;
            for(int i=1; i<=n; i++)
                addedge(s,i,mid);
            for(int i=1; i<=m; i++)
            {
                int x=finds(e[i].u);
                for(int j=1; j<=n; j++)
                    if(x==pre[j]&&!vis[j][e[i].v])
                            {
                                vis[j][e[i].v]=1;
                                addedge(j,n+e[i].v,1);
                            }
            }
            for(int i=n+1; i<=2*n; i++)
                addedge(i,t,mid);
            if(Dinic(s,t)>=mid*n) ans=mid,l=mid+1;
            else r=mid-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 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 u,v,next,cap;
} edge[N*N];
struct E
{
    int u,v,cap;
} e[N*15];
int cnt,head[N],pre[N],gap[N],dis[N],start,End,ans;
int vis[110][110],n,m;
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
}
void addedge(int u,int v,int cap)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].u=v;
    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,f;
    int u,v;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d",&n,&m,&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=2*n+1,ans=End+1;
        int l=0,r=n,ans;
        int tt=cnt;
        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<=m; i++)
            {
                int x=finds(e[i].u);
                for(int j=1; j<=n; j++)
                    if(x==pre[j]&&!vis[j][e[i].v])
                    {
                        vis[j][e[i].v]=1;
                        addedge(j,n+e[i].v,1);
                    }
            }
            for(int i=n+1; i<=2*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、付费专栏及课程。

余额充值