LeetCode -- 785. 判断二分图
深度优先遍历判断二分图
c ++ class Solution { const static int N = 110, M = N * N; int h[N], e[M], ne[M], idx; int st[N]; void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ; } public: bool isBipartite(vector<vector<int>>& graph) { int n = graph.size(); memset(h, -1, sizeof h); for(int i = 0; i < n; i ++ ) { for(auto it : graph[i]) { add(i, it); } } function<bool(int, int)> dfs = [&](int u, int col) -> bool { st[u] = col; for(int i = h[u]; ~i; i = ne[i]) { int j = e[i]; if(!st[j]) { if(!dfs(j, 3 - col)) return false; } else if(st[j] == col) return false; } return true; }; for(int i = 0; i <= n; i ++ ) { if(!st[i]) { if(!dfs(i, 1)) return false; } } return true; } };
java class Solution { int N = 110, M = N * N; int[] h = new int[N], e = new int[M], ne = new int[M]; int idx; int[] color = new int[N]; void add(int a, int b) { e[idx] = b; ne[idx] = h[a]; h[a] = idx ++ ; } boolean dfs(int u, int c) { color[u] = c; for(int i = h[u]; i != -1; i = ne[i]) { int j = e[i]; if(color[j] == 0) { if(!dfs(j, 3 - c)) return false; } else if(color[j] == c) return false; } return true; } public boolean isBipartite(int[][] graph) { Arrays.fill(h, -1); for(int i = 0; i < graph.length; i ++ ) { for(int j = 0; j < graph[i].length; j ++ ) { add(i, graph[i][j]); } } for(int i = 0; i < graph.length; i ++ ) { if(color[i] == 0) { if(!dfs(i, 1)) return false; } } return true; } }
python class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: n = len(graph) color = [0] * n flag = True def dfs(u: int, c: int) -> bool: global flag color[u] = c for it in graph[u]: if color[it] == 0: if not dfs(it, 3 - c): return False elif color[it] == c: return False return True for i in range(n): if color[i] == 0: if not dfs(i, 1): return False return True
golang var color []int func dfs(u int, c int, graph [][]int) bool { color[u] = c for _, it := range graph[u] { if color[it] == 0 { if !dfs(it, 3 - c, graph) { return false } } else if(color[it] == c) { return false } } return true } func isBipartite(graph [][]int) bool { color = make([]int, 110) n := len(graph) for i := 0; i < n; i ++ { fmt.Println(color[i]) if color[i] == 0 { if !dfs(i, 1, graph) { return false } } } return true }
js /** * @param {number[][]} graph * @return {boolean} */ var isBipartite = function(graph) { let len = graph.length; let color = new Array(110).fill(0); let dfs = function (u, c) { color[u] = c; for(let i = 0; i < graph[u].length; i ++ ) { let j = graph[u][i]; if(color[j] === 0) { if(!dfs(j, 3 - c)) return false; } else if(color[j] === c) return false; } return true; } for(let i = 0; i < len; i ++ ) { if(color[i] === 0) { if(!dfs(i, 1)) return false; } } return true; };
分类:
lc练习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义