算法(Algorithms)第4版 练习 1.5.12
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.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列
因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 排序算法总结(C语言版)
排序算法总结(C语言版) 1. 插入排序 1.1 直接插入排序 1.2 Shell排序 2. 交换排序 2.1 冒泡排序 2.2 快速排序 3. 选择 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 《算法》第四版 IDEA 运行环境的搭建
<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
随机推荐
- 使用程序修改系统(IE)代理设置
文章都是发布在github再转到这边的,这边格式可能会乱掉.博客地址:benqy.com 这是本人在做的一个前端开发调试工具(HttpMock),功能是web服务器+http日记+http代理(类似f ...
- css 控制文本显示两行 超过省略号显示
display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; word-break: break-all; ov ...
- 从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named
这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_s ...
- 还在用系统自带的?那你那就OUT了!
相信如今的APP10个里面有九个是有Tabbar的,可是非常多人甚是非常多公司都在用系统自带的tabbar.当然这也不是不能够,并且项目中就那几行代码.效果又一样. 可是,别忘了另一个可是.然并卵.这 ...
- PriorityBlockingQueue优先队列的二叉堆实现
转载请注明原创地址http://www.cnblogs.com/dongxiao-yang/p/6293807.html java.util.concurrent.PriorityBlockingQu ...
- NHibernate 组件基础 (第六篇)
NHibernate 组件基础 (第六篇) 一.组件简介 组件(Component)可以理解为被一个对象所包含的对象而持久化,而并非一个实体.简单说来,假如数据库有FirstName,LastName ...
- instagram架构分析_转
转自:http://www.eit.name/blog/read.php?504 Instagram 团队上个月才迎来第 7 名员工,是的,7个人的团队.作为 iPhone 上最火爆的图片类工具,in ...
- 【Python + Selenium】之unittest测试用例满足条件,进行跳过测试Skip
直接上代码: __author__ = 'zc' import unittest class demoSkipTest(unittest.TestCase): a = 70 b = 50 print( ...
- iOS_3_图片浏览
终于效果图: BeyondViewController.h // // BeyondViewController.h // 03_图片浏览 // // Created by beyond on 14- ...
- Android Material Design-Defining Shadows and Clipping Views(定义阴影和裁剪视图)-(四)
转载请注明出处:http://blog.csdn.net/bbld_/article/details/40539131 翻译自:http://developer.android.com/trainin ...