当前位置:网站首页>Leetcode0785. 判断二分图(medium,二分图,DFS)
Leetcode0785. 判断二分图(medium,二分图,DFS)
2022-04-21 21:54:00 【笨牛慢耕】
目录
1. 问题描述
存在一个 无向图 ,图中有 n 个节点。其中每个节点都有一个介于 0 到 n - 1 之间的唯一编号。给你一个二维数组 graph ,其中 graph[u] 是一个节点数组,由节点 u 的邻接节点组成。形式上,对于 graph[u] 中的每个 v ,都存在一条位于节点 u 和节点 v 之间的无向边。该无向图同时具有以下属性:
- 不存在自环(
graph[u]不包含u)。 - 不存在平行边(
graph[u]不包含重复值)。 - 如果
v在graph[u]内,那么u也应该在graph[v]内(该图是无向图) - 这个图可能不是连通图,也就是说两个节点
u和v之间可能不存在一条连通彼此的路径。
二分图 定义:如果能将一个图的节点集合分割成两个独立的子集 A 和 B ,并使图中的每一条边的两个节点一个来自 A 集合,一个来自 B 集合,就将这个图称为 二分图 。
如果图是二分图,返回 true ;否则,返回 false 。
示例 1:

输入:graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
输出:false
解释:不能将节点分割成两个独立的子集,以使每条边都连通一个子集中的一个节点与另一个子集中的一个节点。
示例 2:

输入:graph = [[1,3],[0,2],[1,3],[0,2]]
输出:true
解释:可以将节点分成两组: {0, 2} 和 {1, 3} 。
提示:
graph.length == n1 <= n <= 1000 <= graph[u].length < n0 <= graph[u][i] <= n - 1graph[u]不会包含ugraph[u]的所有值 互不相同- 如果
graph[u]包含v,那么graph[v]也会包含u
2. 解题分析
这题与本图论基础专题系列的前一道leetcode886就是换汤不换药啊。
只不过本题直接给出的就是邻接表(adjacency list),而leetcode886给的dislikes代表的是edge列表。
具体解题思路请参考leetcode886 (Leetcode0886. 可能的二分法(medium,二分图)).
3. 代码实现
class Solution:
def isBipartite(self, graph: List[List[int]]) -> bool:
n = len(graph)
if n == 1:
return True
color = (n) * [-1]
for k in range(n):
if color[k] < 0:
q = deque([k])
color[k] = 0
while len(q)>0:
node = q.pop()
for adj in graph[node]:
if color[adj] < 0:
color[adj] = 1 - color[node]
q.append(adj)
else:
if color[adj] == color[node]:
return False
return True
执行用时:32 ms, 在所有 Python3 提交中击败了98.25%的用户
内存消耗:15.3 MB, 在所有 Python3 提交中击败了77.72%的用户
回到总目录:笨牛慢耕的Leetcode每日一题总目录(动态更新。。。)
版权声明
本文为[笨牛慢耕]所创,转载请带上原文链接,感谢
https://chenxiaoyuan.blog.csdn.net/article/details/124329377
边栏推荐
猜你喜欢

iphone测试,自定义tabbar的图片会跟着屏幕滑动

Authing 正式加入 W3C 组织,将参与相关国际标准制定

OA form design case display

02.GCC编译器的使用

01_交叉編譯Hello程序

Alibaba cloud OSS, user authentication and patient

Idea operates redis on Linux through jedis; Failed to connect to any host resolved for DNS name

Use try-with-resources or close this “FileOutputStream“

分析师认为三星Galaxy Z Fold 4和Z Flip 4可能比其前代产品更便宜

01_ Cross compile Hello program
随机推荐
During iPhone test, the picture of custom tabbar will slide along the screen
Unity3d C uses the offset of material map to realize the function of unlimited moving background effect of 2D game single background image (including source code)
Travel notes of provincial election and later basic planning
js实现公告自动滚动
Hard core strength, recognized by many parties | cloud expansion technology, as the core manufacturer of RPA, was selected into the 2022 China RPA procurement guide
Makefile file configure executable file and cflags parameter
初识线程安全有这一篇就够了
Online yaml to properties tool
Helm install Jenkins (official)
Jupyter notebook has no run button
Mswep data NC format to TIF format
2022 crane driver (limited to bridge crane) work license question bank and online simulation examination
Pay off 900000 housing loans to get the resignation certificate? Tencent response: inconsistent with the actual situation
Summary of force deduction method 824 - goat Latin
【编程时适合的音乐】
动态规划:完全背包问题
2022安全员-A证考试练习题及在线模拟考试
Ffmpeg serial 3-video decoding
PHP数组函数extract 使用详解
FFmpeg连载1-环境搭建