前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >最大匹配(简单版)

最大匹配(简单版)

作者头像
Lokinli
发布2023-03-09 17:03:46
2790
发布2023-03-09 17:03:46
举报
文章被收录于专栏:以终为始以终为始

二分匹配——最大匹配

代码语言:javascript
复制
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 300;
vector<int>E[maxn];
int used[maxn];
int match[maxn];
int n,m;
void add_edge(int u,int v)
{
    E[u].push_back(v);
    E[v].push_back(u);
}
bool dfs(int x)
{
    used[x] = 1;
    for(int i = 0; i < E[x].size(); i ++)
    {
        int u = E[x][i];
        int v = match[u];
        if(v == -1 || used[v] == -1 && dfs(v))
        {
            match[u] = x;
            match[x] = u;
            return true;
        }
    }
    return false;
}
int max_match()
{
    int res = 0;
    memset(match,-1,sizeof(match));
    for(int i = 1; i <= m; i ++)
    {
        if(match[i] == -1)
        {
            memset(used,-1,sizeof(used));
            if(dfs(i))
                res ++;
        }
    }
    return res;
}
int main()
{
    scanf("%d %d",&n, &m);
    int u,v;
    while(scanf("%d %d",&u,&v)!=EOF)
    {
        add_edge(u,v);
    }
    int ans = max_match();
    printf("%d\n",ans);
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-10-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档