二分图

作者: 啊啊啊哼哼哼 | 来源:发表于2020-01-28 19:37 被阅读0次

一个图是二分图当且仅当图中不含有奇数环

染色法判定二分图:

由于图中不含有奇数换,所以染色过程一定没有矛盾

  • 如果一个图可以用二染色进行染色,没有矛盾,则图是二分图
  • 用到的点:深度优先或者广度优先遍历,链表法存图
  • 注意的点:图不是连通图,所以每一个点都要在主函数遍历一遍
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static int N = 100010;
    static int M = 200010;
    static int[]g = new int[N], color = new int[N];
    static int []e = new int[M], ne = new int[M];

    static int idx = 0;
    static boolean flag = true;
    public static void main(String []args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String []line = reader.readLine().split("\\s+");
        int n = Integer.parseInt(line[0]), m = Integer.parseInt(line[1]);
        while(m-->0){
            String []list = reader.readLine().split("\\s+");
            int a = Integer.parseInt(list[0]), b = Integer.parseInt(list[1]);
            //无向图添加两次边
            add(a,b);
            add(b,a);
        }
        for(int i = 1;i<=n;i++){
            if(color[i]==0){
                if(!dfs(i,1)) {
                    flag = false;
                    break;
                }
            }
        }
        if(flag) System.out.println("Yes");
        else System.out.println("No");
    }

    private static boolean dfs(int id, int colour) {
        if(color[id]==0){
            color[id]=colour;
            for(int i = g[id]; i!= 0;i = ne[i]){
                int tmp = e[i];
                if(!dfs(tmp,3-colour)) return false;
            }
        }
        if (color[id]!=colour) return false;
        return true;
    }

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

匈牙利算法

package graph;

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

public class HungaryAlgorithm {
    static int N = 510;
    static int M = 100010;
    static int[] g = new int[N], match = new int[N];
    static int idx;
    static int res;
    static int[] e = new int[M], ne = new int[M];
    static boolean[] st = new boolean[N];

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] line = reader.readLine().split("\\s+");
        int n1 = Integer.parseInt(line[0]), n2 = Integer.parseInt(line[1]), m = Integer.parseInt(line[2]);
        while (m-- > 0) {
            String[] list = reader.readLine().split("\\s+");
            int a = Integer.parseInt(list[0]), b = Integer.parseInt(list[1]);
            add(a, b);
        }
        for (int i = 1; i <= n1; i++) {
            Arrays.fill(st, false);
            if (find(i)) res++;
        }
        System.out.println(res);
    }

    private static boolean find(int id) {
        for (int i = g[id]; i != 0; i = ne[i]) {
            int tmp = e[i];
            if (!st[tmp]) {
                st[tmp] = true;
                if (match[tmp] == 0 || find(match[tmp])) {
                    match[tmp] = id;
                    return true;
                }
            }
        }
        return false;
    }


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

相关文章

  • 二分匹配 专题整理

    二分匹配学习记录 参考资料 二分图讲解及匈牙利模板 HDU 2444 题意 给出图,求是否二分图,和二分图的最大匹...

  • 【算法篇】二分图匹配之匈牙利算法

    二分图匹配,自然要先从定义入手,那么二分图是什么呢? 二分图: 二分图又称作二部图,是图论中的一种特殊模型。 设G...

  • 算法学习之路|二分图的最大匹配—匈牙利算法(Dfs实现)

    摘要:二分图的概念:二分图又称作二部图,是图论中的一种特殊模型 二分图的概念:二分图又称作二部图,是图论中的一种特...

  • 二分图基础知识

    前言:总结一下二分图相关的知识点 0X00 基础 判断是不是二分图 785. 判断二分图 DFS 遍历所有节点,遍...

  • 基于图的personal rank推荐算法

    背景 用户的行为很容易表示为图定点,边 uesr,item构建图(二分图)二分图:又称为二部图,是图论中的一种特...

  • LeetCode 785. 判断二分图

    题目 785. 判断二分图 描述 给定一个无向图graph,当这个图为二分图时返回true。如果我们能将一个图的节...

  • 二分图

    二分图判定: 题目链接:二分图判定 dfs: 最大匹配: 题目链接:最大匹配-匈牙利算法 dfs: 二维最大匹配:...

  • 二分图

    说说二分图,其实图论的题难点不在用算法,难在如何建图,只有图建好了,剩下的就简单了,在这说说求二分图的算法,即匈牙...

  • 785. 判断二分图

    785. 判断二分图 染色法

  • 二分图

    二分图一些常用结论:最小支配集:V* 中最少的点,关联最多(V-V* )中的点;最小点覆盖:用最少的点去覆盖完所有...

网友评论

      本文标题:二分图

      本文链接:https://www.haomeiwen.com/subject/ocxmthtx.html