日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

LeetCode - 785. Is Graph Bipartite?

發(fā)布時間:2025/5/22 73 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode - 785. Is Graph Bipartite? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

判斷一個給定圖是不是二分圖. 題目提供一個用二維數(shù)組存儲的鄰接表. 常規(guī)的二分圖判斷,點著色.

注意要將圖存入類中,因為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 }

轉(zhuǎn)載于:https://www.cnblogs.com/AntonLiu/p/11288250.html

總結(jié)

以上是生活随笔為你收集整理的LeetCode - 785. Is Graph Bipartite?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。