二分图求最大匹配的简单实现

二分图百度百科:https://baike.baidu.com/item/%E4%BA%8C%E5%88%86%E5%9B%BE/9089095?fr=aladdin

二分图求最大匹配实现起来很容易,解决这类问题需要多层次考虑,先开始的谁都不用考虑,后面开始的需要结合前面已经匹配的结果进行考虑。

 

看不懂的可以留言,看到了即回复。

 

注意:

不要把dfs(mathc[i])和match[i]==0 写反了,因为程序match[i]==0是判断这个i是否已经匹配了,dfs(mathc[i])是当前这个i已经匹配了,但是后来者居上,step把i抢走了,所以i要回娘家告诉爸妈把老公休了。

测试程序:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int n,m;
int sum=0;
int map[100][100];
int match[100];
int book[100];
int dfs(int step)
{
    for(int i=1; i<=n; i++)
    {
        if(map[step][i]==1)
        {
            if(book[i]==0)
            {
                book[i]=1;
                if(match[i]==0 || dfs(match[i]))
                {
                    cout<<"I am:"<<step<<" , my wife:"<<i<<endl;
                    match[i]=step;
                    match[step]=i;
                    return 1;
                }
            }
        }

    }
    return 0;
}

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1; i<=m; i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        map[a][b]=1;
        

 

测试结果:

 

 

如果看不懂上面我写的,可以看看下面的:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int n,m;
int sum=0;
int map[100][100];
int match[100];
int book[100];
int dfs(int step)
{
    cout<<step<<" wait compare"<<endl;
    for(int i=1; i<=n; i++)
    {
        if(map[step][i]==1)
        {
            if(book[i]==0)
            {
                book[i]=1;
                if(match[i]==0)
                {
                    cout<<"I am:"<<step<<" , my wife:"<<i<<endl;
                    match[i]=step;
                    match[step]=i;
                    return 1;
                }
                if(dfs(match[i]))
                {
                    cout<<"I am:"<<step<<" , my wife:"<<i<<endl;
                    match[i]=step;
                    match[step]=i;
                    return 1;
                }
                else
                {
                    cout<<step<<" have compared"<<endl;
                    return 0;
                }
            }
        }

    }
    return 0;
}

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1; i<=m; i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        map[a][b]=1;
        map[b][a]=1;
    }
    for(int i=1; i<=n; i++)
    {
        cout<<"I am:"<<i<<endl;
        memset(book,0,sizeof(book));
        if(dfs(i))
            sum++;
        cout<<endl;
    }
    cout<<sum<<endl;

}


 

测试结果:

 

注意:

不要把dfs(mathc[i])和match[i]==0 写反了,因为程序match[i]==0是判断这个i是否已经匹配了,dfs(mathc[i])是当前这个i已经匹配了,但是后来者居上,step把i抢走了,所以i要回娘家告诉爸妈把老公休了。

写反程序测试:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int n,m;
int map[100][100];
int match[100];
int book[100];
int sum;
int dfs(int step)
{
    cout<<step<<endl;
    for(int i=1; i<=n; i++)
    {
        if(map[step][i]==1)
        {
            if(book[i]==0)
            {
                book[i]=1;
                if(dfs(match[i]||match[i]==0))
                {
                    match[i]=step;
                    match[step]=i;
                    return 1;
                }
            }
        }
    }
    return 0;
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1; i<=m; i++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        map[a][b]=1;
        map[b][a]=1;
    }
    for(int i=1; i<=n; i++)
    {
        memset(book,0,sizeof(book));
        if(dfs(i))
            sum++;
    }
    cout<<sum<<endl;

}



 

测试结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的探索之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值