mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-05 20:06:31 +08:00
Update UnionFind算法应用.md
This commit is contained in:
@ -317,7 +317,7 @@ class Solution {
|
|||||||
public boolean validTree(int n, int[][] edges) {
|
public boolean validTree(int n, int[][] edges) {
|
||||||
// 树的特性:节点数 = 边数 + 1
|
// 树的特性:节点数 = 边数 + 1
|
||||||
if (edges.length != n - 1) return false;
|
if (edges.length != n - 1) return false;
|
||||||
graph_261.Solution.DisjointSet djs = new graph_261.Solution.DisjointSet(n);
|
DisjointSet djs = new DisjointSet(n);
|
||||||
for (int[] edg : edges) {
|
for (int[] edg : edges) {
|
||||||
// 判断连通情况(如果合并的两个点在一个连通分量里,说明有环)
|
// 判断连通情况(如果合并的两个点在一个连通分量里,说明有环)
|
||||||
if (!djs.union(edg[0], edg[1])) return false;
|
if (!djs.union(edg[0], edg[1])) return false;
|
||||||
|
Reference in New Issue
Block a user