LeetCode - 785. Is Graph Bipartite?

判断一个给定图是不是二分图. 题目提供一个用二维数组存储的邻接表. 常规的二分图判断,点着色.

注意要将图存入类中,因为dfs需要访问图中的点.

 1 class Solution {
 2     private int[][] graph;
 3     private boolean[] visited;
 4     private int[] colors;
 5
 6     public boolean isBipartite(int[][] graph) {
 7         this.graph = graph;
 8         int V = graph.length;
 9         visited = new boolean[V];
10         colors = new int[V];
11
12         for (int v = 0; v < V; v++)
13             if (!visited[v])
14                 if(!dfs(v, 0))
15                     return false;
16         return true;
17     }
18
19     private boolean dfs(int v, int color) {
20         visited[v] = true;
21         colors[v] = color;
22         for (int w : graph[v]) {
23             if (!visited[w]) {
24                 if (!dfs(w, 1 - color))
25                     return false;
26             }
27             else if (colors[v] == colors[w])
28                 return false;
29         }
30         return true;
31     }
32 }

原文地址:https://www.cnblogs.com/AntonLiu/p/11288250.html

时间: 2024-11-06 07:29:59

LeetCode - 785. Is Graph Bipartite?的相关文章

[leetcode]785. Is Graph Bipartite? [bai&#39;pɑrtait] 判断二分图

Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0,2], [1,3], [0,2]] Output: true Explanation: The graph looks like this: 0----1 | | | | 3----2 We can divide the vertices into two groups: {0, 2} and {1

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 the graph has one node in A and another node in B.

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 the graph has one node in A and another node in B.

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点.... class Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): if None =

【leetcode】Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh

leetcode -day11 Clone Graph &amp; Palindrome Partitioning I II

 1.Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

[LeetCode] 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 the graph has one node in A and another node in B.

【Leetcode】 Clone Graph

题目链接:https://leetcode.com/problems/clone-graph/ 题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node

Leetcode总结之Graph

package Graph; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Prior