leetcode 785. Is Graph Bipartite?(二分图)

安卓集成华为HMS Scankit实现自定义扫码功能 实现扫码功能 使用的是集成华为扫描的SDK,UI界面借鉴的是谷歌官方的Zxing项目中的自定义view,两者结合实现。 先上效果图: 先在项目根级别(build.gradle)当中配置HMS Core SDK的Maven仓地址。 // 配置HMS Core SDK的Maven仓地址。 maven {url 'https://developer.huawei.com/repo/'} // 配置HMS Core SDK的Maven仓地址。 maven {url 'https://developer.huaw 阅读详情

Given an undirected graph, return true if and only if it is bipartite.

Recall that a graph is bipartite if we can split its set of nodes into two independent subsets A and B, such that every edge in the graph has one node in A and another node in B.

The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists. Each node is an integer between 0 and graph.length - 1. There are no self edges or parallel edges: graph[i] does not contain i, and it doesn’t contain any element twice.

Example 1:
在这里插入图片描述
Input: graph = [[1,3],[0,2],[1,3],[0,2]]
Output: true
Explanation: We can divide the vertices into two groups: {0, 2} and {1, 3}.

Constraints:
1 <= graph.length <= 100
0 <= graph[i].length < 100
0 <= graph[i][j] <= graph.length - 1
graph[i][j] != i
All the values of graph[i] are unique.
The graph is guaranteed to be undirected.

无向图,问图是否为二分图。
二分图可以理解为给图的顶点染两种颜色,一条边的两个顶点不能是同一种颜色。

思路:
假设给图的顶点染两种颜色,分别为颜色1和颜色2
还没有染色的node染上颜色1,那这个node连接的另一头顶点要染上颜色2。
BFS遍历graph,如果中间出现颜色矛盾的情况就返回false。
定义group数组保存每个node的颜色。
注意每个顶点都要遍历。

    public boolean isBipartite(int[][] graph) {
        int n = graph.length;
        int[] group = new int[n];
        for(int i = 0; i < n; i++) {
            if(group[i] == 0 && !bfs(i, graph, n, group)) return false;
        }
        
        return true;
    }
    
    boolean bfs(int cur, int[][] graph, int n, int[] group) {
        
        Queue<Integer> queue = new LinkedList<>();
        
        queue.offer(cur);
        if(group[cur] == 0) group[cur] = 1;
        
        while(!queue.isEmpty()) {
            int node = queue.poll();
            int nextGroup = group[node] == 1 ? 2 : 1;
            for(int nextNode : graph[node]) {
                if(group[nextNode] == 0) {
                    group[nextNode] = nextGroup;
                    queue.offer(nextNode);
                } else if(group[nextNode] != nextGroup) {
                    return false;
                }
            }
        }
        
        return true;
    }

方法2: DFS
因为只有两种颜色,可以利用正负数翻转来预测下一个颜色,如果出现矛盾则返回false。

class Solution {
    public boolean isBipartite(int[][] graph) {
        int n = graph.length;
        int[] colors = new int[n];
        
        for(int i = 0; i < n; i ++) {
            if(colors[i] == 0 && !dfs(graph, colors, i, 1)) return false;
        }
        return true;
    }
    
    boolean dfs(int[][] graph, int[] colors, int i, int color) {
        if(colors[i] != 0) return colors[i] == color;
        colors[i] = color;
        for(int nxt : graph[i]) {
            if(!dfs(graph, colors, nxt, -color)) return false;
        }
        return true;
    }
}
《多模态融合改进》目录一览 | 专栏介绍 :全网 第一份 完整的多模态改进教程,提供《多模态模型改进完整项目包》-开箱即用 在大家订阅专栏后,便可获得多模态模型改进完整项目包-开箱即用,方便简单 阅读详情

相关推荐

ECU-TEST笔记&使用技巧01

说明 1)所述均基于如下环境:ECU-TEST 8.0、Win10系统、NI Veristand 2018(HIL测试)。不排除因为版本及软件环境问题,所述方法不奏效或有更好的方法,欢迎交流,一起提高。 2)本博客以问答形式进行,所述问题均来自于实际测试(包括MIL、HIL测试)遇到的问题及需求,目的是提高测试效率和自动化测试序列的可维护性。 3)本博客尽量从测试方法及ECU-TEST原...

Eigrl的博客 1万+

算法设计与分析(五)

Is Graph Bipartite? 1. 题目 Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it’s set of nodes into two independent subsets A and B...

Vilin的博客 313

sift、surf、orb 特征提取——三维重建

参考文章:[sift、surf、orb 特征提取及最优特征点匹配] 最近刚刚入门三维重建,在SFM算法的代码中注意到了feature_type( feature extraction method)这个参数,想细致了解一下这几种特征提取算法,在这里做一下笔记。 SIFT 简介 SIFT(Scale-Invariant Feature Transform)——尺度不变特征变换,是一种计算机视觉的特征提取算法,用来侦测与描述图像中的局部特征。 实质上,它是在不同的尺度空间上查找关键点(特征点),并计算出关键点的

bb_sy_w的博客 4366

Leetcode 785. Is Graph Bipartite? (python+cpp)

Leetcode 785. Is Graph Bipartite?题目解法: BFS或DFS+染色 题目 解法: BFS或DFS+染色 对于二分图的判断是用经典的染色法。 染色的方式是,看能否用两种颜色将所有节点涂上颜色,并且保证相邻节点都是不一样的颜色。如果可以做到,那么说明图是可以二分的。 这道题目用BFS或者DFS都是可以的,换句话说用队列或者堆栈都可以。如果是BFS,那么在当前节点涂完色时候,你会把他相邻的节点都涂成另一种颜色。如果是DFS的话,当前节点涂完之后,你会把最近的相邻节点涂成另一种颜色

qq_37821701的博客 1185

Rsoft仿真实战:长周期光纤光栅参数优化与模式耦合分析

本文详细介绍了使用Rsoft软件进行长周期光纤光栅(LPFG)参数优化与模式耦合分析的实战方法。通过Rsoft仿真,可以高效优化光栅周期、包层厚度等关键参数,深入分析纤芯模与包层模的耦合特性,为光纤通信和传感应用提供可靠的设计依据。

h0i1j2k3l的博客 1124

从零开始:使用Python实现基于深度强化学习的柔性车间调度系统

本文详细介绍了如何使用Python和深度强化学习(DRL)构建柔性车间调度系统,特别关注图神经网络(GNN)与近端策略优化(PPO)算法的结合应用。通过实战指南,读者将学习从环境准备、异构图构建到PPO算法实现的全流程,最终实现生产效率的显著提升。

weixin_29074295的博客 254

LeetCode //C - 785. Is Graph Bipartite?

Abstract: Determine whether an undirected graph is bipartite. A bipartite graph requires that the nodes can be divided into two sets, and each edge connects nodes in different sets. Use DFS or BFS to traverse the graph, mark the nodes with two colors, and

Made in Code 672

ISP算法:自动白平衡(AWB)

1. 为什么要做白平衡? 人类视觉系统具有颜色恒常性特点,一次人类对物体观察不受光源影响。但是Sensor在不同光线下,物体呈现的颜色不同,在晴朗天空下会偏蓝,在烛光下会偏红。如图: 2.什么是自动白平衡? 为了消除光源对于图像传感器成像的影响,模拟人类视觉系统的颜色恒常性。保证在任何场景下看到的白色是真正的白色。 3. 如何做自动白平衡? 只要乘以一个对角矩阵,便可以进行白平衡...

a200800170331的专栏 1万+

[leetcode]785. Is Graph Bipartite?

[leetcode]785. Is Graph Bipartite? Analysis 出太阳啦~—— [每天刷题并不难0.0] Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it’s set of...

weixin_32135877的博客 247

芯片设计|SoC芯片设计流程 与 软硬件协同设计

SoC通常被称作系统级芯片或者片上系统,作为一个完整的系统,**其包含了硬件和软件两部分内容**。这里所说的硬件指SoC芯片部分,**软件是指运行在SoC芯片上的系统及应用程序**。既然它是由软件和硬件组合而成,则在进行系统设计时,**就必须同时从软件和硬件的角度去考虑。**

叫好与叫座虽然不是对立面,但想在同一个作品中达到双重效果很难。 1833

华为OD机考双机位C卷 - 完美走位 (Java & Python& JS & C/C++ & GO )

华为OD机考双机位C卷 - 完美走位(OD上机考试双机位C卷)。提供Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。

算法大师! 1140

[Leetcode] 785. Is Graph Bipartite? 解题报告

题目: Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that e

魔豆(Magicbean)的博客 1947

【OpenClaw从入门到精通】第12篇:OpenClaw 2026子代理实战全攻略:用ACP架构搭建多智能体协作网络(附完整代码)

本文聚焦OpenClaw 2026.3.2版本默认启用的ACP(Agent Communication Protocol)子代理机制,从核心概念、底层原理到企业级实操全流程拆解。先解析子代理的定义、ACP协议通信原理及主从代理分工逻辑,再提供环境准备的详细步骤(版本验证、配置调整、技能安装);随后通过三个递进式虚拟案例(客服分流、智能文档处理、企业级招聘全流程),提供可直接运行的完整代码、执行结果及技术要点解析;最后补充子代理管理运维、性能优化与避坑指南。

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 2405

Leetcode 785.判断二分图(Is Graph Bipartite?)

Leetcode 785.判断二分图 1 题目描述(Leetcode题目链接)   给定一个无向图graph,当这个图为二分图时返回true。 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图。 graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点。每个节点都是一个在0到graph.length-1之间的整数。这图中没有自环和平行边: graph[i] 中不存在i,并且graph[i]中没有

qq_39378221的博客 462

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

【二分图】LeetCode C++ 785. Is Graph Bipartite? 【Medium】

Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in t...

memcpy0的博客 699

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

算法分析 | leetcode | 785. Is Graph Bipartite?

算法分析 | leetcode | 785. Is Graph Bipartite? 一、问题描述 Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two ...

bai1998的博客 347

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LeetCode-785. Is Graph Bipartite? [C++][Java]

There is anundirectedgraph withnnodes, where each node is numbered between0andn - 1. You are given a 2D arraygraph, wheregraph[u]is an array of nodes that nodeuis adjacent to. More formally, for eachvingraph[u], there is an undirected edge b...

贫道绝缘子的博客 383

【Leetcode】785. Is Graph Bipartite?

题目地址: https://leetcode.com/problems/is-graph-bipartite/ 判断一个图是否是二分图。直接用定义来做就好了。可以从某个点开始做搜索,搜索的同时染色。如果发现染色出矛盾了(也就是相邻点染成了同一个颜色),那么就返回false。 法1:DFS。从某个点开始,做DFS,做的同时顺便把点染色。代码如下: public class Solution { ...

数学、算法爱好者的博客 300

LeetCode 785. Is Graph Bipartite【DFS,二分图】中等

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。

memcpy0的博客 652

LeetCode 785. Is Graph Bipartite?

文章目录题目描述结果知识点实现码前思考代码实现码后反思 题目描述 结果 知识点 图的遍历BFS+DFS、二分图 实现 码前思考 这道题目是我专门找的二分图来做练习的,利用这道题回忆了一下二分图的概念! 其实发现一个套路,当你去解决一道图的时候,发现没有思路,那么基本上 要往DFS或者BFS上面去靠 ,毕竟考点也就那么一些嘛~ 利用二分图的性质:相邻的两个点必须属于两个不同的集合,然后去遍...

yc_cy1999的博客 326

[leetcode]图算法之二分图染色法/BFS的运用785. Is Graph Bipartite?

图算法之二分图染色法/BFS的运用题目解析答案 题目 leetcode入口 解析 二分图,其实就是把点分成两个集合 使用BFS,相邻曾的节点属于不同的集合,如果在BFS的过程中遇到了之前访问过的节点,就说明不能分成两个集合 答案 //BFS class Solution { public: bool isBipartite(vector<vector<int>>&a...

crabstew的博客 738

leetcode 785. Is Graph Bipartite? | 785. 判断二分图(DFS,地图着色)

题目 https://leetcode.com/problems/is-graph-bipartite/ 题解 有点像简化版的地图着色问题。 每走一步,颜色翻转一下,并且染色。 当遇到已经染过色的节点时, 如果颜色正确,则停止此路径 如果颜色错误,则返回 false DFS完成之后,返回 true。 另外,由于本题不保证是连通图,所以对于非连通图来说,可以将两个连通分量独立考察。 详见草稿: class Solution { int N; public boolean isBipa

寒泉 3万+

leetcode785-is graph bipartite 判断二分图

给定一个无向图graph,当这个图为二分图时返回true。 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图。 graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点。每个节点都是一个在0到graph.length-1之间的整数。这图中没有自环和平行边: gr...

一介北漂 229

Weekly Contest 72 leetcode 785. Is Graph Bipartite?

Given a graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the grap

淡然坊 959

[LeetCode 785] Is Graph Bipartite?

Given an undirectedgraph, returntrueif and only if it is bipartite. Recall that a graph isbipartiteif we can split it's set of nodes into two independentsubsets A and B such that every edge in ...

ExcitedZhang的博客 207

【LeetCode】785. Is Graph Bipartite?

Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it’s set of nodes into two independent subsets A and B such that every edge in t...

hcm_0079的博客 262
上一篇: leetcode 743. Network Delay Time(网络延迟时间)
下一篇: log_softmax与softmax的区别
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址