package com.qiusongde;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class UFQuickUnionPathCom { private int[] id;//save the site's parent link(site indexed)
private int count;//number of components public UFQuickUnionPathCom(int n) { count = n; id = new int[n];
for(int i = 0; i < n; i++)
id[i] = i; } public int count() {
return count;
} public boolean connected(int p, int q) {
return find(p) == find(q);
} //adding a loop to link every site on the path
//from p to the root directly to the root
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);//find pRoot
int qRoot = find(q);//find qRoot if(pRoot == qRoot)
return; id[pRoot] = qRoot;
count--;
} @Override
public String toString() {
String s = ""; for(int i = 0; i < id.length; i++) {
s += id[i] + " ";
}
s += "\n" + count + " components"; return s;
} public static void main(String[] args) { //initialize N components
int N = StdIn.readInt();
UFQuickUnionPathCom uf = new UFQuickUnionPathCom(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");
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
10 components
4 3
0 1 2 3 3 5 6 7 8 9
9 components
3 8
0 1 2 8 3 5 6 7 8 9
8 components
6 5
0 1 2 8 3 5 5 7 8 9
7 components
9 4
0 1 2 8 8 5 5 7 8 8
6 components
2 1
0 1 1 8 8 5 5 7 8 8
5 components
8 9 is connected
5 0
0 1 1 8 8 0 5 7 8 8
4 components
7 2
0 1 1 8 8 0 5 1 8 8
3 components
6 1
1 1 1 8 8 0 0 1 8 8
2 components
1 0 is connected
6 7 is connected

输入序列:

10

0 1

1 2

2 3

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

这样产生了长度为4的路径0->1->2->3。

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

  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. Windows 清除系统垃圾文件

    @echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*._m ...

  2. Mac下安装LNMP(Nginx+PHP5.6)环境(转)

    安装Homebrew 最近工作环境切换到Mac,所以以OS X Yosemite(10.10.1)为例,记录一下从零开始安装Mac下LNMP环境的过程 确保系统已经安装xcode,然后使用一行命令安装 ...

  3. linux下OpenSSL的RSA密钥生成

    工具的安装: 一.源码安装 OpenSSL Version:openssl-1.0.0e.tar.gz ------------------------安装: 1.将下载的压缩包放在根目录, 2.在文 ...

  4. oracle中创建dblink

    create database link to_group connect to UCR_GROUP identified by "UCR_GROUPQWER"using '(de ...

  5. Mysql主从配置笔记

    1.配置my.cnf无效,且mysql进程无法启动 从5.1.7版本开始,不再支持my.cnf直接配置master-host等主从相关配置选项(依然支持replicate-do-db).改为使用 CH ...

  6. 让子元素在父元素中水平居中align-items

    做案例中,我们会发现让子元素在父元素中垂直居中,要设置margin和padding等,各种设置才能垂直居中 现在可以使用CSS3中的align-items实现 align-items 定义子元素在父元 ...

  7. mysql 考勤表异常 【待修改】

    有考勤刷卡记录表,表名为attendance ,有如下字段: 姓名 卡号 刷卡时间 刷卡类型 name id time type    张三 59775623 2010-04-01 07:23:37  ...

  8. Hadoop 101: Programming MapReduce with Native Libraries, Hive, Pig, and Cascading

    和Hadoop交互的四种方法: 1. Native Libraries 2. Hive 3. Pig 4. Cascading At a high level, people use the nati ...

  9. Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)

    Windows下Redis的安装使用 0.前言 因为是初次使用,所以是在windows下进行安装和使用,参考了几篇博客,下面整理一下 1.安装Redis 官方网站:http://redis.io/ 官 ...

  10. 【BZOJ4004】[JLOI2015]装备购买 贪心+高斯消元

    [BZOJ4004][JLOI2015]装备购买 Description 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 ( ...