并查集 Union-Find
- 并查集(Union-Find)
-
- 常用模板
- [★ 547. 省份数量](https://leetcode.cn/problems/number-of-provinces/)
- [841. 钥匙和房间](https://leetcode.cn/problems/keys-and-rooms/)
- [990. 等式方程的可满足性](https://leetcode.cn/problems/satisfiability-of-equality-equations/)
- [1319. 连通网络的操作次数](https://leetcode.cn/problems/number-of-operations-to-make-network-connected/)
- [684. 冗余连接](https://leetcode.cn/problems/redundant-connection/)
- 1
-
- [1061. 按字典序排列最小的等效字符串](https://leetcode.cn/problems/lexicographically-smallest-equivalent-string/description/?orderBy=newest_to_oldest)
- [685. 冗余连接 II](https://leetcode.cn/problems/redundant-connection-ii/)
- [2316. 统计无向图中无法互相到达点对数](https://leetcode.cn/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/)
- [1391. 检查网格中是否存在有效路径](https://leetcode.cn/problems/check-if-there-is-a-valid-path-in-a-grid/)
- [1697. 检查边长度限制的路径是否存在](https://leetcode.cn/problems/checking-existence-of-edge-length-limited-paths/)
- [2503. 矩阵查询可获得的最大分数](https://leetcode.cn/problems/maximum-number-of-points-from-grid-queries/)
- [785. 判断二分图](https://leetcode.cn/problems/is-graph-bipartite/)
- [886. 可能的二分法](https://leetcode.cn/problems/possible-bipartition/)
- [765. 情侣牵手](https://leetcode.cn/problems/couples-holding-hands/)
- [839. 相似字符串组](https://leetcode.cn/problems/similar-string-groups/)
- [2076. 处理含限制条件的好友请求](https://leetcode.cn/problems/process-restricted-friend-requests/)
- [2092. 找出知晓秘密的所有专家](https://leetcode.cn/problems/find-all-people-with-secret/)
- [200. 岛屿数量](https://leetcode.cn/problems/number-of-islands/)
- [236. 二叉树的最近公共祖先](https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/)
- [827. 最大人工岛](https://leetcode.cn/problems/making-a-large-island/)
- [695. 岛屿的最大面积](https://leetcode.cn/problems/max-area-of-island/)
- [1905. 统计子岛屿](https://leetcode.cn/problems/count-sub-islands/)
- [1020. 飞地的数量](https://leetcode.cn/problems/number-of-enclaves/)
- [130. 被围绕的区域](https://leetcode.cn/problems/surrounded-regions/)
- [1254. 统计封闭岛屿的数目](https://leetcode.cn/problems/number-of-closed-islands/)
- [1559. 二维网格图中探测环](https://leetcode.cn/problems/detect-cycles-in-2d-grid/)
- [1579. 保证图可完全遍历](https://leetcode.cn/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/)
- 2
-
- [★ 778. 水位上升的泳池中游泳](https://leetcode.cn/problems/swim-in-rising-water/)
- [★ 1970. 你能穿过矩阵的最后一天](https://leetcode.cn/problems/last-day-where-you-can-still-cross/)
- [★ 1584. 连接所有点的最小费用](https://leetcode.cn/problems/min-cost-to-connect-all-points/)
- [★ 1631. 最小体力消耗路径](https://leetcode.cn/problems/path-with-minimum-effort/)
- [★ 803. 打砖块](https://leetcode.cn/problems/bricks-falling-when-hit/)
- 3 合并交换
- 4 合并横纵坐标
- 5
-
- [★399. 除法求值](https://leetcode.cn/problems/evaluate-division/)
- [2157. 字符串分组](https://leetcode.cn/problems/groups-of-strings/)
- [★1627. 带阈值的图连通性](https://leetcode.cn/problems/graph-connectivity-with-threshold/)
- [952. 按公因数计算最大组件大小](https://leetcode.cn/problems/largest-component-size-by-common-factor/)
- [2709. 最大公约数遍历](https://leetcode.cn/problems/greatest-common-divisor-traversal/)
- [1998. 数组的最大公因数排序](https://leetcode.cn/problems/gcd-sort-of-an-array/)
- [★ 128. 最长连续序列](https://leetcode.cn/problems/longest-consecutive-sequence/)
- 6
- 7
-
- [▲ 1632. 矩阵转换后的秩](https://leetcode.cn/problems/rank-transform-of-a-matrix/)
- [▲ 2003. 每棵子树内缺失的最小基因值](https://leetcode.cn/problems/smallest-missing-genetic-value-in-each-subtree/)
- [▲ 924. 尽量减少恶意软件的传播](https://leetcode.cn/problems/minimize-malware-spread/)
- [928. 尽量减少恶意软件的传播 II](https://leetcode.cn/problems/minimize-malware-spread-ii/)
- 8
- 9 向右合并
- 10
- [1569. 将子数组重新排序得到同一个二叉查找树的方案数](https://leetcode.cn/problems/number-of-ways-to-reorder-array-to-get-same-bst/)
- [LCS 03. 主题空间](https://leetcode.cn/problems/YesdPw/)
- [LCP 71. 集水器](https://leetcode.cn/problems/kskhHQ/)
- [LCP 49. 环形闯关游戏](https://leetcode.cn/problems/K8GULz/)
- 总结
- 启发式合并
- 扩展域并查集
并查集(Union-Find)
并查集是解决 动态连通性 问题的一类非常高效的数据结构。
连通: 无向图上两点 u, v 之间存在路径,称这两个点是连通的。
连通分量: 无向图的每个极大连通块是这张无向图的连通分量。
例:547. 省份数量
无向图:省 连通分量,isConnected 邻接矩阵
森林,树:由 子结点 找到 父结点。
1、初始化
使用森林(若干棵树)来表示图的动态连通性,用 数组 来具体实现这个森林。
设定树的每个结点有一个指针指向其父结点,如果是根结点的话,这个指针指向自己。

parent [],索引:城市编号,值:父结点,开始为自己和自己相连。