力扣labuladong——一刷day79

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣785. 判断二分图](#一、力扣785. 判断二分图)
  • [二、力扣886. 可能的二分法](#二、力扣886. 可能的二分法)

前言


给你一幅「图」,请你用两种颜色将图中的所有顶点着色,且使得任意一条边的两个端点的颜色都不相同,你能做到吗? 这就是图的「双色问题」,其实这个问题就等同于二分图的判定问题,如果你能够成功地将图染色,那么这幅图就是一幅二分图,反之则不是:

一、力扣785. 判断二分图

java 复制代码
class Solution {
    boolean ok = true;
    boolean[] visited;
    boolean[] color;
    public boolean isBipartite(int[][] graph) {
        int n = graph.length;
        visited = new boolean[n];
        color = new boolean[n];
        for(int i = 0; i < n; i ++){
            if(!visited[i]){
                traverse(graph, i);
            }
        }
        return ok;
    }
    public void traverse(int[][] graph, int v){
        if(!ok){
            return;
        }
        visited[v] = true;
        for(int e : graph[v]){
            if(!visited[e]){
                color[e] = !color[v];
                traverse(graph,e);
            }else{
                if(color[e] == color[v]){
                    ok = false;
                    return;
                }
            }
        }
    }
}

二、力扣886. 可能的二分法

java 复制代码
class Solution {
    boolean ok = true;
    boolean[] visited;
    boolean[] color;
    public boolean possibleBipartition(int n, int[][] dislikes) {
        visited = new boolean[n];
        color = new boolean[n];
        List<Integer>[] graph = builderGraph(dislikes, n);
        for(int i = 0; i < n; i ++){
            if(!visited[i]){
                BFS(graph, i);
            }
        }
        return ok;
    }
    public List<Integer>[] builderGraph(int[][] dislikes,int n){
        List<Integer>[] graph = new LinkedList[n];
        for(int i = 0; i < n ; i ++){
            graph[i] = new LinkedList<>();
        }
        for(int[] arr : dislikes){
            int to = arr[0]-1;
            int from = arr[1]-1;
            graph[to].add(from);
            graph[from].add(to);
        }
        return graph;
    }
    public void BFS(List<Integer>[] graph, int v){
        if(!ok){
            return;
        }
        Deque<Integer> deq = new LinkedList<>();
        deq.offerLast(v);
        visited[v] = true;
        while(!deq.isEmpty()){
            int cur = deq.pollFirst();
            for(int e : graph[cur]){
                if(!visited[e]){
                    visited[e] = true;
                    color[e] = !color[cur];
                    deq.offerLast(e);
                }else{
                    if(color[e] == color[cur]){
                        ok = false;
                        return;
                    }
                }
            }
        }
    }
}
相关推荐
karmueo4615 分钟前
视频序列和射频信号多模态融合算法Fusion-Vital解读
算法·音视频·多模态
小汉堡编程1 小时前
数据结构——vector数组c++(超详细)
数据结构·c++
程序员张32 小时前
Maven编译和打包插件
java·spring boot·maven
写代码的小球3 小时前
求模运算符c
算法
ybq195133454313 小时前
Redis-主从复制-分布式系统
java·数据库·redis
weixin_472339464 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
小毛驴8504 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
雾里看山5 小时前
顺序表VS单链表VS带头双向循环链表
数据结构·链表
DKPT5 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
好奇的菜鸟6 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea