力扣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;
                    }
                }
            }
        }
    }
}
相关推荐
MicrosoftCloud6 分钟前
数据结构 07(上)|查找:顺序、折半、分块,ASL 到底怎么
数据结构·查找·顺序查找·折半查找·分块查找·平均查找长度·asl
街头便衣11 分钟前
OpenRouter周榜复算:164万亿token,输出2.55%,DeepSeek账单占1.4%
android·java·开发语言·人工智能·语言模型·开源
Mortalbreeze13 分钟前
0 - 1 BFS 算法详解:从基本原理到经典题目建模
数据结构·c++·算法·图论·宽度优先
mister_guo16 分钟前
Spring Boot 配置文件读取的几种方式:用法、原理与常见误区
java·spring boot
余槐i17 分钟前
虚拟线程上线后吞吐反而掉了:Spring Boot 3 里 synchronized 锁住载体线程的定位与修复
java·spring boot·性能优化·虚拟线程·jdk 21
要开心吖ZSH20 分钟前
DDD(领域驱动设计)篇三:实战对照——同一个订单业务,两种架构怎么写
java·架构·领域模型·ddd·领域驱动设计·设计思想
飞Link2 小时前
什么是 PX4?一文了解 PX4 在空地协同中的作用
算法·无人机
一去不复返的通信er8 小时前
力扣-图论-岛屿数量
算法·leetcode·图论
考研保研资料分享10 小时前
985工科跨保上财应用统计:数学笔试、匿名面试与机试复盘
面试·职场和发展