问题描述
w星球的一个种植园,被分成 m * n 个小格子(东西方向m行,南北方向n列)。每个格子里种了一株合根植物。
这种植物有个特点,它的根可能会沿着南北或东西方向伸展,从而与另一个格子的植物合成为一体。
如果我们告诉你哪些小格子间出现了连根现象,你能说出这个园中一共有多少株合根植物吗?
输入格式
第一行,两个整数m,n,用空格分开,表示格子的行数、列数(1<m,n<1000)。
接下来一行,一个整数k,表示下面还有k行数据(0<k<100000)
接下来k行,第行两个整数a,b,表示编号为a的小格子和编号为b的小格子合根了。
格子的编号一行一行,从上到下,从左到右编号。
比如:5 * 4 的小格子,编号:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
样例输入
5 4
16
2 3
1 5
5 9
4 8
7 8
9 10
10 11
11 12
10 14
12 16
14 18
17 18
15 19
19 20
9 13
13 17
样例输出
5
样例说明
其合根情况参考下图
import java.util.*;
class UnionFind { //并查集
private int[] roots;
private int[] weight;
private int size;
public UnionFind(int size) {
this.size = size;
roots = new int[size];
weight= new int[size];
for(int i=0;i<size;i++) {
roots[i] = i;
weight[i] = 1;
}
}
public int Find(int element) { //查找根
while(roots[element]!=element) {
element = roots[element];
}
return element;
}
public boolean isConnect(int firstElement, int secondElement) { //判断是否是同根
return roots[firstElement] == roots[secondElement];
}
public void unionElement(int firstElement, int secondElement) { //根的链接
int firstRoot = Find(firstElement);
int secondRoot = Find(secondElement);
if(firstRoot == secondRoot)
return ;
if(weight[firstRoot] > weight[secondRoot]) {
roots[secondRoot] = firstRoot;
weight[firstRoot] += weight[secondRoot];
}
else{//weight[firstRoot] <= weight[secondRoot]
roots[firstRoot] = secondRoot;
weight[secondRoot] += weight[firstRoot];
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int count = m*n;
UnionFind root = new UnionFind(m*n);
int num = sc.nextInt();
for(int i=0;i<num;i++) {
int x = sc.nextInt()-1;
int y = sc.nextInt()-1;
if(!root.isConnect(root.Find(x),root.Find(y))) {
root.unionElement(x, y);
count--;
}
}
System.out.println(count);
}
并查集(Java实现)
http://www.cnblogs.com/noKing/p/8018609.html
这是我学习并查集的教程,讲述的非常详细,感谢此教程的帮助!!!