力扣labuladong——一刷day79

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


前言


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

一、力扣785. 判断二分图

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. 可能的二分法

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;
                    }
                }
            }
        }
    }
}

相关推荐

  1. labuladong——day79

    2023-12-25 12:28:01       36 阅读
  2. labuladong——day70

    2023-12-25 12:28:01       39 阅读
  3. labuladong——day74

    2023-12-25 12:28:01       36 阅读
  4. labuladong——day75

    2023-12-25 12:28:01       39 阅读
  5. labuladong——day76

    2023-12-25 12:28:01       52 阅读
  6. labuladong——day77

    2023-12-25 12:28:01       33 阅读
  7. labuladong——day78

    2023-12-25 12:28:01       36 阅读
  8. labuladong——day68

    2023-12-25 12:28:01       37 阅读
  9. labuladong——day67

    2023-12-25 12:28:01       33 阅读
  10. labuladong——day69

    2023-12-25 12:28:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-25 12:28:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-25 12:28:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-25 12:28:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-25 12:28:01       18 阅读

热门阅读

  1. qt的血泪教训——地图存储与绘制

    2023-12-25 12:28:01       39 阅读
  2. Redis-实战案例

    2023-12-25 12:28:01       46 阅读
  3. sql server 增删改查(基本用法)

    2023-12-25 12:28:01       37 阅读
  4. 【AI】人工智能本地环境集成安装

    2023-12-25 12:28:01       33 阅读
  5. 5G RedCap:轻量5G技术的新宠

    2023-12-25 12:28:01       39 阅读
  6. IP地址与MAC地址的区别与联系

    2023-12-25 12:28:01       33 阅读
  7. python 文本文件的读取

    2023-12-25 12:28:01       43 阅读
  8. Sublime Text快捷命令

    2023-12-25 12:28:01       38 阅读
  9. 华为OD笔试2023C卷命题规律解读

    2023-12-25 12:28:01       54 阅读
  10. vue ant v-decorator的使用

    2023-12-25 12:28:01       40 阅读