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): 2923    Accepted Submission(s): 969



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
 

题意:如果女孩X与男孩Y不会争吵,那么Y可以做X的男朋友;如果女孩X与女孩Z是好朋友,Z与男孩W不会争吵,那么W也可以做X的女朋友。现在问,有多少种完全不同的完美匹配方案。(完全不同的意思是任意两个方案之间没有任何相同的匹配对)


分析:本题有两种解法:二分图完美匹配,二分 + 网络流。然而我却只会用二分图完美匹配来做,而不会用网络流来解,蒟蒻还是要继续成长……

二分图完美匹配:女孩之间有关系用并查集来实现。建好图后,找到一次完美匹配,然后把匹配的边全部删掉,ans++;直到找不到完美匹配为止。

二分 + 最大流:方案最多可以为n,所有二分的区间是[0,n]。每次二分时,加入超级源点s,s指向所有女孩,容量为mid;每一个女孩指向她能够一起的男生(注意去重),容量为1;加入超级汇点t,使得所有男孩指向t,容量为mid。然后判断最大流是否等于mid * n,若相等,ans=mid,L=mid+1;否则,R=mid-1。


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3081


代码清单:

二分图完美匹配

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

#define end() return 0

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxn = 100 + 5;
const int maxv = 10000 + 5;
const int INF = 0x7f7f7f7f;

int T;
int N,M,F,x,y;
int a[maxv],b[maxv];
bool graph[maxn][maxn];
int match[maxn];
bool vis[maxn];

int father[maxn];
int Find(int x){ return father[x]!=x ? father[x]=Find(father[x]) : father[x]; }

void init(){
    for(int i=0;i<maxn;i++) father[i]=i;
    memset(graph,false,sizeof(graph));
}

void input(){
    scanf("%d%d%d",&N,&M,&F);
    for(int i=1;i<=M;i++){
        scanf("%d%d",&a[i],&b[i]);
    }
    for(int i=0;i<F;i++){
        scanf("%d%d",&x,&y);
        father[Find(y)]=Find(x);
    }
}

bool dfs(int u){
    for(int v=1;v<=N;v++){
        if(!vis[v]&&graph[u][v]){
            vis[v]=true;
            if(match[v]==-1 || dfs(match[v])){
                match[v]=u;
                return true;
            }
        }
    }return false;
}

int work(){
    
    //构图
    for(int i=1;i<=M;i++){
        for(int j=1;j<=N;j++){
            if(Find(j)==Find(a[i])&&!graph[j][b[i]])
                graph[j][b[i]]=true;
        }
    }

    int ans=0;
    while(true){
        int ret=0;
        memset(match,-1,sizeof(match));
        for(int i=1;i<=N;i++){
            memset(vis,false,sizeof(vis));
            if(dfs(i)) ret++;
        }
        if(ret<N) break;
        ans++;
        for(int i=1;i<=N;i++){
            if(match[i]) graph[match[i]][i]=false;
        }
    }
    return ans;
}

void solve(){
    printf("%d\n",work());
}

int main(){
    scanf("%d",&T);
    while(T--){
        init();
        input();
        solve();
    }end();
}



二分 + 最大流

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

#define end() return 0

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxN = 100 + 5;
const int maxn = 200 + 5;
const int maxv = 10000 + 5;
const int INF = 0x7f7f7f7f;

struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

struct dinic{
    int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点
    vector<Edge>edge;//边表。edge[e]和edge[e^1]互为反向弧
    vector<int>G[maxn];//邻接表。G[i][j]表示结点i的第j条边在e数组的序号
    bool vis[maxn]; //bfs用
    int d[maxn]; //从起点到i的距离
    int cur[maxn]; //当前弧下标

    void init(int n,int s,int t){
        this -> n = n;
        this -> s = s;
        this -> t = t;
        for(int i=0;i<=n;i++) G[i].clear();
        edge.clear();
    }

    void addEdge(int from,int to,int cap){
        edge.push_back(Edge(from,to,cap,0));
        edge.push_back(Edge(to,from,0,0));
        m=edge.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs(){
        memset(vis,false,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=true;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge& e=edge[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow){ //只考虑残量网络中的弧
                    vis[e.to]=true;
                    d[e.to]=d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int x,int a){
        if(x==t||a==0) return a;
        int flow=0,f;
        for(int& i=cur[x];i<G[x].size();i++){ // & -> 从上次考虑的弧
            Edge& e=edge[G[x][i]];
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edge[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }

    int maxflow(){
        int flow=0;
        while(bfs()){
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,INF);
        }
        return flow;
    }
};

int T;
int N,M,F,X,Y;
int a[maxv],b[maxv];
bool vis[maxN][maxN];

int tail;
dinic dc;

int father[maxN];
int Find(int x){ return father[x]!=x ? father[x]=Find(father[x]) : father[x]; }

void init(){
    for(int i=0;i<maxN;i++){
        father[i]=i;
    }
}

void input(){
    scanf("%d%d%d",&N,&M,&F);
    for(int i=1;i<=M;i++){
        scanf("%d%d",&a[i],&b[i]);
    }
    for(int i=0;i<F;i++){
        scanf("%d%d",&X,&Y);
        father[Find(Y)]=Find(X);
    }
}

int work(){
    tail=2*N+1;
    int le=0,ri=N;
    int mid,ans=0;
    while(le<=ri){
        mid=(le+ri)>>1;
        dc.init(tail+1,0,tail);

        memset(vis,false,sizeof(vis));
        for(int i=1;i<=M;i++){
            for(int j=1;j<=N;j++){
                if(Find(j)==Find(a[i])&&!vis[j][b[i]]){ //判重
                    vis[j][b[i]]=true;
                    dc.addEdge(j,b[i]+N,1);
                }
            }
        }

        for(int i=N+1;i<=2*N;i++){
            dc.addEdge(0,i-N,mid);
            dc.addEdge(i,tail,mid);
        }

        int flow=dc.maxflow();
        if(flow==mid*N){
            ans=mid;
            le=mid+1;
        }
        else ri=mid-1;
    }
    return ans;
}

void solve(){
    printf("%d\n",work());
}

int main(){
    scanf("%d",&T);
    while(T--){
        init();
        input();
        solve();
    }end();
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值