GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/BFS/785.Is-Graph-Bipartite at master · wisdompeak/LeetCode

Expand file tree

/

785.Is-Graph-Bipartite

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

785.Is-Graph-Bipartite

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
785.Is-Graph-Bipartite.cpp
785.Is-Graph-Bipartite.cpp
 
 
785.Is-Graph-Bipartite_UnionFind.cpp
785.Is-Graph-Bipartite_UnionFind.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

785.Is-Graph-Bipartite

对于任意一个定点(比如说节点0),假设将其归入A组,那么所有0节点的相邻节点必定都要归入B组;同理0节点的相邻节点的相邻节点,必定又都要归入A组。由此可以发现,我们从0节点开始做BFS的遍历,扩展的时候以A组/B组的次序交替标记,就可以将与0节点在同一个连通图内的所有节点进行分组。如果在BFS过程中遇到已经分组的节点并且发现矛盾之处,那么就可以返回false。

注意,以上的BFS是针对同一个连通图而言。如果整个graph有若干个连通图(彼此之间不相连),那么就需要遍历所有的节点(查找未曾访问过的节点作为一个连通图的起始节点),以保证所有的连通图都被逐一访问和BFS遍历过。

当然,本题也可以用并查集来做。根据题意,对于graph[i]内的所有节点{j},它们必定属于同一集合。因此我们Union每个graph[i]的点集,并查看过程中是否遇到任何矛盾。

Leetcode Link

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.