二分图:染色法、匈牙利算法

什么是二分图

二分图是可以将集合中所有的点划分为两部分,使得仅在集合与集合之间存在边,而集合内部不存在边。

二分图的重要性质:

当且仅当图中不含有奇数环(环中边的个数为奇数)时。

必要性证明:
o---o----o---o
1   2    3   1
假设二分图中存在上图所示的1,2,3三个点构成奇数环,那么由二分图定义可知:
假设1属于集合L,那么2一定属于集合R,3一定属于集合L,可推出1一定属于集合R
这与假设矛盾,所以二分图中不存在奇数环。

染色法

用于判断一个给定的图是不是二分图。不存在奇数环的染色过程是不矛盾的,作为上述的充分性证明。

AcWing 860. 染色法判定二分图

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010, M = 2 * N;
int n, m;
int h[N], e[M], ne[M], idx;
int color[N];

void add(int a, int b){
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

bool dfs(int u, int c){
    color[u] = c;
    
    for(int i = h[u]; i != -1; i = ne[i])
    {
        int j = e[i];
        if(!color[j])
        {
            if(!dfs(j, 3 - c)) return false;
        }
        
        else if(color[j] == c) return false;
    }
    
    return true;
}

int main(){
    cin >> n >> m;
    memset(h, -1, sizeof h);
    
    while(m--)
    {
        int a, b;
        cin >> a >> b;
        add(a, b);
        add(b, a);
    }
    
    bool flag = true;
    for(int i = 1; i <= n; i++)
    {
        if(!color[i])
        {
            if(!dfs(i, 1))
            {
                flag = false;
                break;
            }
        }
    }
    
    if(flag) puts("Yes");
    else puts("No");
    
    return 0;
}

二分图的最大匹配

二分图的匹配:给定一个二分图 G ,在 G 的一个子图 M 中,
 M 的边集 {E} 中的任意两条边都不依附于同一个顶点,则称 M 是一个匹配。

二分图的最大匹配:所有匹配中包含边数最多的一组匹配被称为二分图的最大匹配,其边数即为最大匹配数。

二分图的最大匹配可以看作两个集合互相配对,在不“脚踏两条船”的前提下的最大匹配数是多少。

匈牙利算法用于求二分图的最大匹配

又称“不撞南墙不回头”、“宁可做错不可错过”算法。

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 510, M = 100010;
int h[N], e[M], ne[M], idx;
int n1, n2, m;
int match[N];
bool st[N];

void add(int a, int b){
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

bool find(int x){
    for(int i = h[x]; i != -1; i = ne[i])
    {
        int j = e[i];
        if(!st[j])
        {
            st[j] = true;
            if(match[j] == 0 || find(match[j]))
            {
                match[j] = x;
                return true;
            }
        }
    }
    
    return false;
}

int main(){
    cin >> n1 >> n2 >> m;
    memset(h, -1, sizeof h);
    
    while(m--)
    {
        int a, b;
        cin >> a >> b;
        add(a, b);
    }
    
    int res = 0;
    for(int i = 1; i <= n1; i++)
    {
        memset(st, false, sizeof st);
        if(find(i)) res ++;
    }
    
    printf("%d\n", res);
    
    return 0;
}

AcWing 1394. 完美牛棚

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 210;
bool g[N][N];
int n, m;
bool st[N];
int match[N];

bool find(int x){
    for(int i = 1; i <= m; i++)
    {
        if(!st[i] && g[x][i])
        {
            st[i] = true;
            if(match[i] == 0 || find(match[i]))
            {
                match[i] = x;
                return true;
            }
        }
    }
    
    return false;
}

int main(){
    cin >> n >> m;
    
    for(int i = 1; i <= n; i++)
    {
        int s;
        cin >> s;
        while(s--)
        {
            int x;
            cin >> x;
            g[i][x] = true;
        }
    }
    
    int res = 0;
    for(int i = 0; i <= n; i++)
    {
        memset(st, false, sizeof st);
        if(find(i)) res ++;
    }
    
    printf("%d", res);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值