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

算法(Algorithms)第4版 练习 1.5.13的更多相关文章

  1. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...

  2. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  3. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  4. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  5. 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列

    因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...

  6. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  7. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

  8. 算法(第四版)C#题解——2.1

    算法(第四版)C#题解——2.1   写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...

  9. 《算法》第四版 IDEA 运行环境的搭建

    <算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...

  10. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

随机推荐

  1. StringUtils工具类详解

    StringUtils判断字符串大概有四种方法: 下面是 StringUtils 判断是否为空的示例: 判断是否为空,但是要注意,空格不算空,这个最好能不用则不用. StringUtils.isEmp ...

  2. 0x00 使用Ant 设置项目

    1. Ant 简介: Ant 是一款广泛使用的流行的开源构建工具,它用Java语言编写. 2.Ant官网: Ant官网:http://ant.apache.org/ 3.设置环境变量: 新建 Vari ...

  3. window平台安装 MongoDB(二)

    MongoDB提供了可用于32位和64位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB预编译二进制包下载地址:http://www.mongodb.org/downloads ...

  4. Codeforces 38G Queue 伸展树

    题目链接:点击打开链接 题意: 给定n个人来排队 每一个人有2个參数.身份优先级和脸皮厚度 == 来的那个人会排到队尾 假设这个人的优先级比他前面那个人的优先级大就会和前面那个人交换位置. 交换一次脸 ...

  5. diamond源码阅读-目录监控

    PathNode(Path)StandardWatchEventKind(WatchEvent)Watchable(WatchKey WatchService WatchEvent)WatchKey( ...

  6. iOS开发---业务逻辑

    iOS开发---业务逻辑   1. 业务逻辑 iOS的app开发的最终目的是要让用户使用, 用户使用app完成自己的事就是业务逻辑, 业务逻辑的是最显眼开发工作.但是业务逻辑对于开发任务来说, 只是露 ...

  7. iOS 10 中引入了 Message 框架

    WWDC 2016 上最重磅的消息之一就是在 iOS 10 中引入了 Message 框架.开发者现在可以为苹果内置的 Messages 应用开发扩展啦.通过开发一个应用扩展,你可以让用户跟应用在 M ...

  8. linux php.ini又一次载入问题

    今天发现自己server改动php.ini之后无法又一次载入! .无法使用php-fpm reload,奇怪.! 后来查了一下.能够使用 /etc/init.d/php-fpm reload 来又一次 ...

  9. 使用3DES+Base64来加密传输iOS应用数据

    本文转载至 http://www.erblah.com/post/objective-c/shi-yong-3des-base64lai-jia-mi-chuan-shu-iosying-yong-s ...

  10. 九度OJ 1208:10进制 VS 2进制 (进制转换)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2040 解决:612 题目描述: 对于一个十进制数A,将A转换为二进制数,然后按位逆序排列,再转换为十进制数B,我们乘B为A的二进制逆序数. ...