package com.qiusongde;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class UFWQuickUnionPathCom { private int[] id;//parent link(site indexed)
private int[] treesize;//size of component for roots(site indexed)
private int count;//number of components public UFWQuickUnionPathCom(int N) { count = N; id = new int[N];
for(int i = 0; i < N; i++)
id[i] = i; treesize = new int[N];
for(int i = 0; i < N; i++)
treesize[i] = 1; } public int count() {
return count;
} public boolean connected(int p, int q) {
return find(p) == find(q);
} public int find(int p) { int root = p;//initialize root //find root(id[p] save the parent of p)
while(root != id[root])
root = id[root]; //link every site from p to root to root
while(id[p] != root) {//parent isn't root
int nextp = id[p];
id[p] = root;//link directly to root
p = nextp;
} return root; } public void union(int p, int q) { int pRoot = find(p);
int qRoot = find(q); if(pRoot == qRoot)
return; //make smaller root point to larger one
if(treesize[pRoot] < treesize[qRoot]) {
id[pRoot] = qRoot;
treesize[qRoot] += treesize[pRoot];
} else {
id[qRoot] = pRoot;
treesize[pRoot] += treesize[qRoot];
} count--; } @Override
public String toString() {
String s = ""; for(int i = 0; i < id.length; i++) {
s += id[i] + " ";
}
s += "\n"; for(int i = 0; i < treesize.length; i++) {
s += treesize[i] + " ";
}
s += "\n" + count + " components"; return s;
} public static void main(String[] args) { //initialize N components
int N = StdIn.readInt();
UFWQuickUnionPathCom uf = new UFWQuickUnionPathCom(N);
StdOut.println(uf); while(!StdIn.isEmpty()) { int p = StdIn.readInt();
int q = StdIn.readInt(); if(uf.connected(p, q)) {//ignore if connected
StdOut.println(p + " " + q + " is connected");
StdOut.println(uf);
continue;
} uf.union(p, q);//connect p and q
StdOut.println(p + " " + q);
StdOut.println(uf);
} } }

测试结果:

0 1 2 3 4 5 6 7 8 9
1 1 1 1 1 1 1 1 1 1
10 components
4 3
0 1 2 4 4 5 6 7 8 9
1 1 1 1 2 1 1 1 1 1
9 components
3 8
0 1 2 4 4 5 6 7 4 9
1 1 1 1 3 1 1 1 1 1
8 components
6 5
0 1 2 4 4 6 6 7 4 9
1 1 1 1 3 1 2 1 1 1
7 components
9 4
0 1 2 4 4 6 6 7 4 4
1 1 1 1 4 1 2 1 1 1
6 components
2 1
0 2 2 4 4 6 6 7 4 4
1 1 2 1 4 1 2 1 1 1
5 components
8 9 is connected
0 2 2 4 4 6 6 7 4 4
1 1 2 1 4 1 2 1 1 1
5 components
5 0
6 2 2 4 4 6 6 7 4 4
1 1 2 1 4 1 3 1 1 1
4 components
7 2
6 2 2 4 4 6 6 2 4 4
1 1 3 1 4 1 3 1 1 1
3 components
6 1
6 2 6 4 4 6 6 2 4 4
1 1 3 1 4 1 6 1 1 1
2 components
1 0 is connected
6 6 6 4 4 6 6 2 4 4
1 1 3 1 4 1 6 1 1 1
2 components
6 7 is connected
6 6 6 4 4 6 6 6 4 4
1 1 3 1 4 1 6 1 1 1
2 components

最新文章

  1. POJ2104 K-TH NUMBER 传说中的主席树
  2. MapReduce实现TopK的示例
  3. LMAX Disruptor – High Performance, Low Latency and Simple Too 转载
  4. javascript eval 执行过程
  5. const char*、char*、char* const、char[]、string的区别
  6. 【转】Nginx 服务器安装及配置文件详解
  7. 应用git(SSH设置)
  8. 高级I/O函数(3)-tee、fcntl函数
  9. thinkphp pdo 重写问题
  10. JS的事件动态绑定机制
  11. String类为什么要用final修饰(面试回答)
  12. PS图层混合算法之四(亮光, 点光, 线性光, 实色混合)
  13. PS 滤镜—— 径向模糊
  14. 003-005:Java平台相关的面试题
  15. 玩转MQTT-阿里云之MQTT使用
  16. iOS项目之“返回”手势操作相关
  17. Mac安装MySQL数据库
  18. 使用 SSH 和 SFTP 协议
  19. Keras2.2 predict和fit_generator的区别
  20. 在oracle电子商务套件中输出信息

热门文章

  1. How to Use HTML5 FUll Screen API(怎样使用HTML5全屏接口)
  2. oracle如何进行索引监控分析和优化
  3. 搭建redis集群遇到的坑
  4. 使用phpize建立php扩展(Cannot find config.m4)
  5. LNMP环境搭建之php安装,wordpress博客搭建
  6. rsync的介绍及参数详解,配置步骤,工作模式介绍
  7. C语言基础知识【判断】
  8. window下python安装pip
  9. python 基础 9.10 删除数据
  10. 【BZOJ3993】[SDOI2015]星际战争 二分+最大流