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. hdu 1159(Common Subsequence)简单dp,求出最大的公共的字符数

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. Python之比较运算符

    python中的比较运算符有8个. 运算 | 含义=============< | 小于<= | 小于等于> | 大于>= |大于等于== | 等于!= |不等于is | 是i ...

  3. NHibernate 映射基础(第三篇) 简单映射、联合主键

    NHibernate 映射基础(第三篇) 简单映射.联合主键 NHibernate完全靠配置文件获取其所需的一切信息,其中映射文件,是其获取数据库与C#程序关系的所有信息来源. 一.简单映射 下面先来 ...

  4. windows upd广播包无法发送到局域网解决方法

    不能发送广播包的电脑和可以发送广播报的主机对比,发现不能发送广播报的主机上都有安装虚拟机,也有虚拟网卡,将所有的虚拟网卡关闭,然后再进行测试,都正常了,无论是Win7,Win10还是Xp. 禁用VMw ...

  5. maven 工程聚合插件

    <!-- war包生成插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <a ...

  6. centos7 安装vnc服务

    一,安装服务器端 1,添加用户: # useradd vnc # passwd vnc 2,安装gnome桌面 # yum groupinstall "GNOME Desktop" ...

  7. 解决php函数json_encode转换后中文被编码为unicode

    大家都知道使用函数json_encode()可以方便快捷地将数组进行json编码转换,但是如果数组值存在着中文,json_encode会将中文转换为unicode编码,例如: <?PHP $ar ...

  8. Tomcat startup.bat启动隐藏弹出的信息窗口

    to make tomcat to use javaw.exe instead of java.exe using some startup parameter or environment vari ...

  9. 【BZOJ3230】相似子串 后缀数组+二分+RMQ

    [BZOJ3230]相似子串 Description Input 输入第1行,包含3个整数N,Q.Q代表询问组数.第2行是字符串S.接下来Q行,每行两个整数i和j.(1≤i≤j). Output 输出 ...

  10. Spring中的国际化资源以及视图跳转

    一.SpringMVC对国际化的支持 SpringMVC进行资源国际化主要是通过ResourceBundleMessageSource实现的,xml如下配置: <bean id="me ...