当前位置:网站首页>并查集框架
并查集框架
2022-07-22 02:28:00 【UniversalNature】
class UF {
// 连通分量个数
private int count;
// 存储每个节点的父节点
private int[] parent;
// n 为图中节点的个数
public UF(int n) {
this.count = n;
parent = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
// 将节点 p 和节点 q 连通
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ)
return;
parent[rootQ] = rootP;
// 两个连通分量合并成一个连通分量
count--;
}
// 判断节点 p 和节点 q 是否连通
public boolean connected(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
return rootP == rootQ;
}
public int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
// 返回图中的连通分量个数
public int count() {
return count;
}
}
边栏推荐
- 1. Meta function and type_ traits
- ROS学习(28)Web GUI
- ClickHouse相关SQL总结:创建表、分区/主键/排序设置、删除表、删除分区、修改表字段
- ES6——模块
- When configuring Eureka, the status displays the computer name instead of localhost and IPADDR, which is the local IP
- 码蹄集 - MT3182 - 填矩阵
- The cluster planned in tidb includes 6 dB, 3 PD and 3 kV. When the application is disconnected, which address is it connected to?
- adb常见命令
- harbor+trivy的安装使用——筑梦之路
- 【06】指令跳转:原来if...else就是goto
猜你喜欢
Bi analytical thinking of business intelligence: cash cycle of manufacturing industry (II)
LeetCode 0814. 二叉树剪枝
PLT draw and save the results
Basic concept of Nacos and single machine startup
ROS学习(28)Web GUI
第3章——创建与维护MySQL数据表
Let security move | no matter what industry network architecture, these six tactics win the target
FPGA image processing learning face recognition
Halcon displays point clouds in TXT file format
Cookies and sessions
随机推荐
df. Describe() detailed + usage + Example
How to select current probe
第3章——创建与维护MySQL数据表
牛客2022暑期集训第一场C题 Grab the Seat! 题解
ClickHouse相关SQL总结:创建表、分区/主键/排序设置、删除表、删除分区、修改表字段
[C language - program compilation] how does the line by line code get to an executable program step by step?
Pyside2 as a simple browser
What is exploratory testing? What are the methods of exploratory testing?
AutoLabel(自动标签)
halcon 使用txt文件格式显示点云
OUU益生菌精耕胃肠健康,获奖天猫国际微生态创新大会
Golang language cli Library
mysql安装失败,不知道什么原因
harbor+trivy的安装使用——筑梦之路
res中values-swxxdp计算
df.drop_duplicates() 详解+用法
Let security move | no matter what industry network architecture, these six tactics win the target
Bi analytical thinking of business intelligence: cash cycle of manufacturing industry (II)
AIDL示例
ES6——模块