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. CentOs yum源安装 nginx

    1 更新源 [root@server ~]#rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.no ...

  2. 【问题记录】springmvc国际化问题

    异常-Cannot change HTTP accept header - use a different locale resolution strategy springmvc国际化时,local ...

  3. Java设计模式透析之 —— 策略(Strategy)

    今天你的leader兴致冲冲地找到你,希望你能够帮他一个小忙.他如今急着要去开会.要帮什么忙呢?你非常好奇. 他对你说.当前你们项目的数据库中有一张用户信息表.里面存放了非常用户的数据.如今须要完毕一 ...

  4. nginx 的uri、request_uri 区别

    在nginx中有几个关于uri的变量,包括$uri $request_uri $document_uri,下面看一下他们的区别 : $request_uri: /stat.php?id=1585378 ...

  5. linux 中vim学习与总结

    平常使用vim总是忘记快捷键,在这里做一个总结一下比较常用的快捷把,省的每次都要去查. h : 向左移动一个字符(←) j : 向上移动一个字符(↑) k : (↓) l : (→) ctrl+f : ...

  6. Java 异常介绍

    Java标准库内建了一些通用的异常,这些类以 Throwable 为顶层父类.Throwable又派生出 Error 类和 Exception 类. 错误:Error类以及他的子类的实例,代表了JVM ...

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

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

  8. iOS 递归锁

    原理:递归锁也是通过 pthread_mutex_lock 函数来实现,在函数内部会判断锁的类型.NSRecursiveLock 与 NSLock 的区别在于内部封装的 pthread_mutex_t ...

  9. iOS --创建文件夹 ,删除文件夹

    //创建文件夹 --> 返回 文件夹 - (NSString *)pathToPatientPhotoFolder { NSString *documentsDirectory = [NSSea ...

  10. 关于angularjs的model的一些问题

    有的时候 在一些页面中 我们会需要用到弹出的模态框,这里主要是使用angularjs的uimodel. 页面效果如下: 首先我们需要在JS的controller中导入$uibModal模块. HTML ...