当前位置:网站首页>Leetcode0785. Judgement bipartite graph (DFS)
Leetcode0785. Judgement bipartite graph (DFS)
2022-04-21 21:57:00 【Slow ploughing of stupid cattle】
Catalog
1. Problem description
There is one. Undirected graph , The picture shows n Nodes . Each of these nodes has a value between 0 To n - 1 Unique number between . Give you a two-dimensional array graph , among graph[u] Is an array of nodes , By nodes u Consists of adjacent nodes . Formally , about graph[u] Each of the v , There is one at node u And nodes v The undirected edge between . The undirected graph also has the following properties :
- There is no self ring (
graph[u]It doesn't containu). - There are no parallel edges (
graph[u]Does not contain duplicate values ). - If
vstaygraph[u]Inside , thatuIt should be ingraph[v]Inside ( The graph is undirected ) - This graph may not be a connected graph , That is, two nodes
uandvThere may not be a path connecting each other .
Bipartite graph Definition : If the node set of a graph can be divided into two independent subsets A and B , And make one of the two nodes of each edge in the graph come from A aggregate , One from B aggregate , This graph is called Bipartite graph .
If the graph is bipartite , return true ; otherwise , return false .
Example 1:

Input :graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
Output :false
explain : Nodes cannot be divided into two independent subsets , So that each edge connects a node in one subset with a node in another subset .
Example 2:

Input :graph = [[1,3],[0,2],[1,3],[0,2]]
Output :true
explain : Nodes can be divided into two groups : {0, 2} and {1, 3} .
Tips :
graph.length == n1 <= n <= 1000 <= graph[u].length < n0 <= graph[u][i] <= n - 1graph[u]Will not containugraph[u]All values Different from each other- If
graph[u]containv, thatgraph[v]It will also includeu
2. Problem solving analysis
This problem is the same as the previous one in this series of basic topics of graph theory leetcode886 Just change the soup without changing the dressing .
But what this question gives directly is the adjacency table (adjacency list), and leetcode886 Given dislikes It stands for edge list .
Please refer to... For specific solutions leetcode886 (Leetcode0886. Possible dichotomy (medium, Bipartite graph )).
3. Code implementation
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
Execution time :32 ms, In all Python3 Defeated in submission 98.25% Users of
Memory consumption :15.3 MB, In all Python3 Defeated in submission 77.72% Users of
Go back to the general catalogue : Slow ploughing by stupid cattle Leetcode General catalogue of daily questions ( Dynamic update ...)
版权声明
本文为[Slow ploughing of stupid cattle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212154017389.html
边栏推荐
- 数据库小练习
- 国产API管理神器Eolink,我爱了
- 【C语言进阶9——指针的进阶(6)- 回调函数】
- 2022 high altitude installation, maintenance and demolition of examination question bank and simulated examination
- 面试必刷算法TOP101之背包九讲篇 TOP14
- 在线YAML转Properties工具
- 数据库事务学习总结
- FFmpeg连载2-分离视频和音频
- [C language advanced level 9 - pointer advanced level (6) - callback function]
- 外包学生管理系统的架构文档
猜你喜欢

软件测试分类与软件测试的原则

在线CSV转YAML工具

可视化图表告诉你,《人世间》《余生,请多指教》到底爆没爆?

Ffmpeg serial 1 - environment construction

UML integrated design example

Pay off 900000 housing loans to get the resignation certificate? Tencent response: inconsistent with the actual situation

软件的生命周期

一加连发两款耳机产品:充电10分钟 听歌20小时

Leetcode0785. 判断二分图(medium,二分图,DFS)

EventBridge 集成云服务实践
随机推荐
Use try-with-resources or close this “FileOutputStream“
Relationship between RFCs
数据库事务学习总结
【JVM】10道不得不会的JVM面试题
Jz19 regular expression matching
Mongo geonear query Chinese fuzzy search in PHP
Browser principle interview questions
Online yaml to properties tool
软件的生命周期
【史上最全 BAT 必问高并发总结】
数据库小练习
WPF data-driven method for modifying binding
How does wechat applet realize the function of jumping from commodity list to commodity details page
Idea operates redis on Linux through jedis; Failed to connect to any host resolved for DNS name
Huayun actively responded to the anti epidemic call of Hefei high tech Zone: practicing social responsibility and contributing to the strength of science and technology enterprises
华云积极响应合肥高新区抗疫号召:践行社会责任 贡献科技企业力量
Push to origin/master was rejected:报错解决办法
2022 high altitude installation, maintenance and demolition of examination question bank and simulated examination
一篇彻底搞懂-->shell脚本
ES6 how to find the intersection of two arrays