Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-05-20 01:37:08
https://leetcode.com/problems/is-graph-bipartite/description/
785. Is Graph Bipartite?
给你一个无向图,判断他是否是二分图。
Example 1:
https://assets.leetcode.com/uploads/2020/10/21/bi2.jpg
Input: graph = [[1,2,3],[0,2],[0,1,3],[0,2]]
Output: false
Explanation: There is no way to partition the nodes into two independent sets
such that every edge connects a node in one and a node in the other.
Example 2:
https://assets.leetcode.com/uploads/2020/10/21/bi1.jpg
Input: graph = [[1,3],[0,2],[1,3],[0,2]]
Output: true
Explanation: We can partition the nodes into two sets: {0, 2} and {1, 3}.
思路:
1.如果一个图是二分图,他可以被分成左右两边且同一边的节点彼此都不连通。
2.所以我们可以对任意点的周围着上不同颜色,如果碰到已经着色过且颜色相同的节点
时,表示这个图必定不是二分图。
3.着色可以用DFS或BFS实现。
Java Code:
作者: Che31128 (justjoke)   2023-05-20 01:59:00
最近好像都是graph :0

Links booklink

Contact Us: admin [ a t ] ucptt.com