第三章图论(十二)

二分图

例题:257. 关押罪犯
在这里插入图片描述
怨气值数据范围是1~109,暗示着我们用二分,为了让监狱内部的怨气值越小,则需要尽量把怨气值大的罪犯分开

  • check(x):表示将任意怨气值大于 x 的两名罪犯放在两个监狱,且两个监狱内部的最大怨气值均不超过 x ,符合返回 true,符合返回 false
  • check(x) 函数的实现,验证该图中是否为一个二分图,即监狱内部怨气值小于 x 的边均去掉,用染色法验证

注意:两个罪犯的怨气值最小的情况下是 1,若监狱内部发生冲突事件怨气值的最大值一定大于等于 1,而本年内监狱中未发生任何冲突事件,输出是 0,因此二分的初始范围是 l = 0,r = 109.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static int n, m;
    static int N = 20010;
    static int M = 200010;
    static int[] h = new int[N];
    static int[] w = new int[M];
    static int[] e = new int[M];
    static int[] ne = new int[M];
    static int idx = 0;
    static int[] color = new int[N]; //0 - 未染,1 - 黑色,2 - 白色

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

    private static boolean check(int x) {
        Arrays.fill(color, 0);//初始化未染色
        Queue<Integer> q = new LinkedList<Integer>();
        for (int i = 1; i <= n; i++) {
            if (color[i] == 0) {
                color[i] = 1;
                q.add(i);
                while (!q.isEmpty()) {
                    int t = q.poll();
                    for (int j = h[t]; j != -1; j = ne[j]) {
                        if (w[j] <= x) continue;//若怨气值小于mid则不连边
                        int k = e[j];
                        if (color[k] == 0) {
                            color[k] = 3 - color[t];
                            q.add(k);
                        } else if (color[k] == color[t]) return false;
                    }
                }
            }
        }
        return true;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s1 = br.readLine().split(" ");
        n = Integer.parseInt(s1[0]);
        m = Integer.parseInt(s1[1]);
        Arrays.fill(h, -1);
        while (m-- > 0) {
            String[] s2 = br.readLine().split(" ");
            int a = Integer.parseInt(s2[0]);
            int b = Integer.parseInt(s2[1]);
            int c = Integer.parseInt(s2[2]);
            add(a, b, c);
            add(b, a, c);
        }
        int l = 0, r = 1000000000;
        while (l < r) {
            int mid = l + r >> 1;
            if (check(mid)) r = mid;
            else l = mid + 1;
        }
        System.out.println(l);
    }
}

例题 :372. 棋盘覆盖
在这里插入图片描述
————————————————————————————————————————————————————

  • 由于每个骨牌只会由连续的两个方块进行搭配,在方块的坐标的行列之和中,一定是偶数和奇数的搭配,即在图中每个蓝色方块只会和周围的白色方块组成一个骨牌,每一个白色方块只会和周围的蓝色方块组成一个骨牌,求组合的最大数,因此题目求的是一个二分图的最大匹配问题
  • 只需求所有偶数方块的最大匹配之和即可,即偶数方块向奇数方块连一条有向边

在这里插入图片描述

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 110;
    static int n, m;
    static boolean[][] g = new boolean[N][N];//该位置不能踩
    static boolean[][] st = new boolean[N][N];
    static int[] dx = {-1, 0, 1, 0};
    static int[] dy = {0, 1, 0, -1};
    static Pair[][] match = new Pair[N][N];

    private static boolean find(int x, int y) {
        for (int i = 0; i < 4; i++) {
            int a = x + dx[i];
            int b = y + dy[i];
            if (a <= 0 || a > n || b <= 0 || b > n) continue;
            if (st[a][b] || g[a][b]) continue;
            st[a][b] = true;
            Pair t = match[a][b];
            if (t.x == 0 || find(t.x, t.y)) {
                t.x = x;
                t.y = y;
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        n = scan.nextInt();
        m = scan.nextInt();
        while (m-- > 0) {
            int x = scan.nextInt();
            int y = scan.nextInt();
            g[x][y] = true;
        }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                match[i][j] = new Pair(0, 0);

        int res = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++) {   //只需求所有偶数方块的最大匹配之和即可
                if ((i + j) % 2 == 0 && !g[i][j]) {
                    for (int k = 1; k <= n; k++) Arrays.fill(st[k], false);
                    if (find(i, j)) res++;
                }
            }

        System.out.println(res);

    }
}

class Pair {
    int x;
    int y;

    Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

例题:376. 机器任务
在这里插入图片描述
最小点覆盖:在给定的边中,每条边连着两个点,每条边至少选择1个点,最小能选多少个点

在二分图中,最大匹配数 == 最小点覆盖

题目描述到每台机器每次转换模式都需要启动一次,每个任务要么在A机器的a[i]模式进行,要么在B机器的b[i]模式进行,模式a[i]和模式b[i]连上一条边,即每条边至少选择1个模式(点),最少能选多少个模式(点),即求最小点覆盖问题

注意:两台机器一开始的模式是0,因此每个任务如果可以在模式为0的情况下进行,则不需要进行重启,可以舍去

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 110;
    static int n, m, k;
    static boolean[] st = new boolean[N];
    static int[] match = new int[N];
    static boolean[][] g = new boolean[N][N];//连边

    private static boolean find(int x) {
        for (int i = 0; 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;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (true) {
            n = scan.nextInt();
            if (n == 0) break;
            Arrays.fill(match, 0);
            for (int i = 0; i < n; i++) Arrays.fill(g[i], false);
            m = scan.nextInt();
            k = scan.nextInt();
            while (k-- > 0) {
                int t = scan.nextInt();
                int a = scan.nextInt();
                int b = scan.nextInt();
                if (a == 0 || b == 0) continue; //若一开始的状态是0,则不需要重启
                g[a][b] = true;
            }

            int res = 0;
            for (int i = 1; i < n; i++) {
                Arrays.fill(st, false);
                if (find(i)) res++;
            }
            System.out.println(res);
        }
    }
}

例题:378. 骑士放置
在这里插入图片描述
最大独立集:选出最多的点使得选出的点之间没有边
在二分图中,求最大独立集 n - m
< == > 去掉最少的点,将所有边都破坏掉
< == > 找最小的点覆盖 m
< == > 找最大匹配 m

最大匹配数 = 最小点覆盖 = 总点数 - 最大独立集

两个格子的马若能互相攻击,则这两个格子能连上一条边,与棋盘覆盖 的题目相似,若两只马能够互相攻击则两只马的格子一定是两种类型的格子,如下图所示,选出最多个格子,使得选出的格子之间没有边,即求最大独立集问题
在这里插入图片描述

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int N = 110;
    static int n, m, cnt;
    static boolean[][] g = new boolean[N][N];
    static boolean[][] st = new boolean[N][N];
    static Pair[][] match = new Pair[N][N];
    static int[] dx = {-2, -1, 1, 2, 2, 1, -1, -2};
    static int[] dy = {1, 2, 2, 1, -1, -2, -2, -1};

    private static boolean find(int x, int y) {
        for (int i = 0; i < 8; i++) {
            int a = x + dx[i];
            int b = y + dy[i];
            if (a <= 0 || a > n || b <= 0 || b > m) continue;
            if (st[a][b] || g[a][b]) continue;

            st[a][b] = true;
            Pair t = match[a][b];
            if (t.x == 0 || find(t.x, t.y)) {
                t.x = x;
                t.y = y;
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        n = scan.nextInt();
        m = scan.nextInt();
        cnt = scan.nextInt();//不能使用的格子
        for (int i = 0; i < cnt; i++) {
            int a = scan.nextInt();
            int b = scan.nextInt();
            g[a][b] = true;
        }

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                match[i][j] = new Pair(0, 0);

        int res = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++) {
                if ((i + j) % 2 == 0 && !g[i][j]) {
                    for (int k = 1; k <= n; k++) Arrays.fill(st[k], false);
                    if (find(i, j)) res++;
                }
            }

        System.out.println(n * m - cnt - res);
    }
}

class Pair {
    int x, y;

    Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

例题:379. 捉迷藏
在这里插入图片描述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    static int N = 210;
    static int M = 30010;
    static int n;
    static int m;
    static boolean[][] d = new boolean[N][N];
    static boolean[] st = new boolean[N];
    static int[] match = new int[N];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s1 = br.readLine().split(" ");
        n = Integer.parseInt(s1[0]);
        m = Integer.parseInt(s1[1]);
        while (m-- > 0) {
            String[] s2 = br.readLine().split(" ");
            int a = Integer.parseInt(s2[0]);
            int b = Integer.parseInt(s2[1]);
            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++) {
            Arrays.fill(st, false);
            if (find(i)) res++;
        }
        System.out.println(n - res);
    }

    private static boolean 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值