并查集
1. 并查集介绍
1 | int[] data = new int[10]; |
2. 典型例题
1202. 交换字符串中的元素
1 | class Solution { |
684. 冗余连接
1 | class Solution { |
990. 等式方程的可满足性
1 | class Solution { |
3. 最小生成树
利用并查集解决最小生成树问题。
思路:使用克鲁斯卡尔算法(加边)然后利用并查集判断子图是否连通。
```java
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int c = in.nextInt();
int[] data = new int[n+1];
for (int i = 1; i <= n; i++){
data[i] = i;
}
PriorityQueue<int []> queue = new PriorityQueue<>(Comparator.comparingInt(a -> a[2]));
// 最小生成树问题
for (int i = 0; i < n * (n - 1) / 2; i++){
int s = in.nextInt();//起点
int t = in.nextInt();//终点
int p = in.nextInt();//距离
queue.add(new int[]{s,t,p*c});
}
int ans = 0;
while (!queue.isEmpty()){
int[] edge = queue.poll();
int s = edge[0], t = edge[1];
if (find(data,s) == find(data,t)){
continue;
}
merge(data,s,t);
ans += edge[2];
}
System.out.println(ans);
}
public static int find(int[] data, int a){
if (data[a] == a){
return a;
}else{
data[a] = find(data,data[a]);
}
return data[a];
}
public static void merge(int[] data, int a, int b){
int x = find(data,a);
int y = find(data,b);
if (x != y){
data[x] = y;
}
}
}