寒假:Day27

Day27

二分图问题
在这里插入图片描述

257. 关押罪犯 - AcWing题库

二分图之染色法问题,二分答案,然后用染色法check这个二分图的可行性即可

#include<bits/stdc++.h>
using namespace std;
const int N = 20010, M = 200010;

int n, m;
int h[N], e[M], ne[M], w[M], idx;
int color[N]; // 0表示未染色,1表示白色,2表示黑色

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

bool dfs(int u, int c, int mid)
{
    color[u] = c; // 先给这个点染色
    for (int i = h[u]; ~i; i = ne[i]) // 遍历它的所有邻点
    {
        int j = e[i];
        if (w[i] <= mid) continue; // 如果边权小于mid,则无所谓
        if (color[j]) // 如果当前点已经被染了同一个颜色,则返回false
            if (color[j] == c) return false;
        else if (!dfs(j, 3 - c, mid)) return false; // 如果没有染色,则继续深搜
    }
    return true; // 全程没有矛盾,则返回true
}

bool check(int mid) // 染色的过程
{
    memset(color, 0, sizeof color); // 先把之前的染色清空
    for (int i = 1; i <= n; i++)
        if (color[i] == 0) // 如果当前点没有染色
            if (!dfs(i, 1, mid)) return false; // 先默认染白色,有矛盾返回false
    return true; // 如果全程没有问题,返回true
}

int main(void)
{
    cin >> n >> m;
    memset(h, -1, sizeof h);
    
    while (m--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c), add(b, a, c);
    }
    
    int l = 0, r = 1e9;
    while (l < r) // 二分模板
    {
        int mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    cout << l << endl ;
    return 0;
}

372. 棋盘覆盖 - AcWing题库

二分图之最大匹配数问题,我们把每一个格子根据横纵坐标之和分为奇点和偶点,然后每一个奇点都只能和一个相邻的偶点匹配,所以我们只需要枚举奇点或偶点找出最大匹配即可

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;

typedef pair<int, int> PII;
const int N = 110;

int n, m;
PII match[N][N]; // 记录格子匹配的格子
bool g[N][N], st[N][N]; // g表记录坏格子,st记录当前格子是否被用过
int dx[] ={-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};

bool find(int x, int y)
{
    for (int i = 0; i < 4; i++) // 遍历当前格子的四个方向
    {
        int a = x + dx[i], b = y + dy[i];
        if (a < 1 || a > n || b < 1 || b > n) continue; // 越界情况
        if (st[a][b] || g[a][b]) continue; // 如果当前格子被用过了or是坏格子
       
        st[a][b] = true; // 更新标记
        PII t = match[a][b]; // t是它的匹配格子
        if (t.x == 0 || find(t.x, t.y)) // 如果它没有匹配格子or它匹配的格子能找到别的格子匹配
        {
            match[a][b] = {x, y}; // 更新匹配
            return true;
        }
    }
    return false;
}

int main(void)
{
    cin >> n >> m;
    while (m--)
    {
         int x, y;
         cin >> x >> y;
         g[x][y] = true; // 坏格子先标记上
    }
    
    int res = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if ((i + j) % 2 && !g[i][j]) // 如果当前是奇点且是好格子
            {
                memset(st, 0, sizeof st); // 重置st,默认全部格子都没用过
                if (find(i, j)) res ++; // 如果它能找到匹配的偶点,更新答案
            }
    cout << res << endl ;
    return 0;
}

376. 机器任务 - AcWing题库

二分图之最小点覆盖问题,但其实就是把每一个任务看作是从 a [ i ] a[i] a[i] b [ i ] b[i] b[i] 的一条边,然后找整个图中囊括这些边的最小点覆盖,而这个最小点覆盖恰好等于二分图最大匹配数

#include<bits/stdc++.h>
using namespace std;
const int N = 110;

int n, m, k;
bool g[N][N], 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;
            int t = match[i];
            if (t == 0 || find(t))
            {
                match[i] = x;
                return true;
            }
        }
    return false;
}

int main(void)
{
    while (cin >> n, n)
    {
        cin >> m >> k;
        memset(g, 0, sizeof g);
        memset(match, 0, sizeof match);
        
        while (k--)
        {
            int t, a, b;
            cin >> t >> a >> b;
            if (!a || !b) continue; // 如果a或b等于0的话,那么就不需要重启,可以不连边
            g[a][b] = true;
        }
        
        int res = 0;
        for (int i = 1; i < n; i++)
        {
            memset(st, 0, sizeof st);
            if (find(i)) res ++;
        }
        cout << res << endl ;
    }
    return 0;
}

378. 骑士放置 - AcWing题库

二分图之最大独立集问题,最大独立集 = 总点数 - 最大匹配数

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 110;

int n, m, k;
bool g[N][N], st[N][N];
PII match[N][N];

int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};

bool find(int x, int y)
{
    for (int i = 0; i < 8; i++)
    {
        int a = x + dx[i], b = y + dy[i];
        if (a < 1 || a > n || b < 1 || b > m) continue;
        if (g[a][b] || st[a][b]) continue;
        
        st[a][b] = true;
        PII t = match[a][b];
        if (t.x == 0 || find(t.x, t.y))
        {
            match[a][b] = {x, y};
            return true;
        }
    }
    return false;
}

int main(void)
{
    cin >> n >> m >> k;
    for (int i = 0; i < k; i++)
    {
        int a, b;
        cin >> a >> b;
        g[a][b] = true;
    }
    int res = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if ((i + j) % 2 && !g[i][j]) 
            {
                memset(st, 0, sizeof st);
                if (find(i, j)) res ++;
            }    
    cout << n * m - k - res << endl ;
    return 0;
}

379. 捉迷藏 - AcWing题库

最小路径覆盖:不走重复点的最少遍历图的路径数,等于总点数减去最大匹配数

最小可重复路径覆盖:可以走重复点的最少遍历图的路径数,需要先将旧图跑一遍Floyd算法求闭包后求出新图,在新图求一遍最小路径覆盖即可

二分图之最小可重复路径覆盖,模板题

#include<bits/stdc++.h>
using namespace std;

const int N = 210;

int n, m;
bool d[N][N], st[N];
int match[N];

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

int main(void)
{
    cin >> n >> m;
    while (m--)
    {
        int a, b;
        cin >> a >> b;
        d[a][b] = true;
    }
    
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                d[i][j] |= d[i][k] & d[k][j];
        
    int res = 0;        
    for (int i = 1; i <= n; i++)
    {
        memset(st, 0, sizeof st);
        if (find(i)) res++;
    }
    cout << n - res << endl ;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值