给一个图中的n个节点, 记为 1 到 n . 在开始的时候图中没有边。
你需要完成下面两个方法:
- connect(a, b), 添加连接节点 a, b 的边.
- query(a, b), 检验两个节点是否联通
样例
5 // n = 5
query(1, 2) 返回 false
connect(1, 2)
query(1, 3) 返回 false
connect(2, 4)
query(1, 4) 返回 true
代码
public class ConnectingGraph {
private int[] father = null;
private int find(int x) {
if (father[x] == x) {
return x;
}
return father[x] = find(father[x]);
}
// 初始化时父结点都是自己
public ConnectingGraph(int n) {
// initialize your data structure here.
father = new int[n + 1];
for (int i = 1; i <= n; ++i)
father[i] = i;
}
// 连接两结点即为并查集合并操作
public void connect(int a, int b) {
int root_a = find(a);
int root_b = find(b);
if (root_a != root_b)
father[root_a] = root_b;
}
// 查询两结点是否相连,即为并查集查询两结点是否拥有相同父结点
public boolean query(int a, int b) {
int root_a = find(a);
int root_b = find(b);
return root_a == root_b;
}
}