算法(Algorithms)第4版 练习 1.5.4
代码实现:
package com.qiusongde; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class UFWeightedQuickUnion { private int[] id;//parent link(site indexed)
private int[] treesize;//size of component for roots(site indexed)
private int count;//number of components private static int totalcost = 0;//for the test of array access public UFWeightedQuickUnion(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) { while(p != id[p]) {
p = id[p];
totalcost += 2;
}
totalcost++; return p; } 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];
totalcost += 5;
} else {
id[qRoot] = pRoot;
treesize[pRoot] += treesize[qRoot];
totalcost += 5;
} 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();
UFWeightedQuickUnion uf = new UFWeightedQuickUnion(N);
StdOut.println(uf);
StdOut.println(); 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("totalcost: " + totalcost);
StdOut.println();
continue;
} uf.union(p, q);//connect p and q
StdOut.println(p + " " + q);
StdOut.println(uf);
StdOut.println("totalcost: " + totalcost);
StdOut.println();
} } }
reference input:
10
4 3
3 8
6 5
9 4
2 1
8 9
5 0
7 2
6 1
1 0
6 7
结果:
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
totalcost: 9 3 8
0 1 2 4 4 5 6 7 4 9
1 1 1 1 3 1 1 1 1 1
8 components
totalcost: 22 6 5
0 1 2 4 4 6 6 7 4 9
1 1 1 1 3 1 2 1 1 1
7 components
totalcost: 31 9 4
0 1 2 4 4 6 6 7 4 4
1 1 1 1 4 1 2 1 1 1
6 components
totalcost: 40 2 1
0 2 2 4 4 6 6 7 4 4
1 1 2 1 4 1 2 1 1 1
5 components
totalcost: 49 8 9 is connected
totalcost: 55 5 0
6 2 2 4 4 6 6 7 4 4
1 1 2 1 4 1 3 1 1 1
4 components
totalcost: 68 7 2
6 2 2 4 4 6 6 2 4 4
1 1 3 1 4 1 3 1 1 1
3 components
totalcost: 77 6 1
6 2 6 4 4 6 6 2 4 4
1 1 3 1 4 1 6 1 1 1
2 components
totalcost: 90 1 0 is connected
totalcost: 98 6 7 is connected
totalcost: 104
worst-case input:
8
0 1
2 3
4 5
6 7
0 2
4 6
0 4
结果:
0 1 2 3 4 5 6 7
1 1 1 1 1 1 1 1
8 components 0 1
0 0 2 3 4 5 6 7
2 1 1 1 1 1 1 1
7 components
totalcost: 9 2 3
0 0 2 2 4 5 6 7
2 1 2 1 1 1 1 1
6 components
totalcost: 18 4 5
0 0 2 2 4 4 6 7
2 1 2 1 2 1 1 1
5 components
totalcost: 27 6 7
0 0 2 2 4 4 6 6
2 1 2 1 2 1 2 1
4 components
totalcost: 36 0 2
0 0 0 2 4 4 6 6
4 1 2 1 2 1 2 1
3 components
totalcost: 45 4 6
0 0 0 2 4 4 4 6
4 1 2 1 4 1 2 1
2 components
totalcost: 54 0 4
0 0 0 2 0 4 4 6
8 1 2 1 4 1 2 1
1 components
totalcost: 63
算法(Algorithms)第4版 练习 1.5.4的更多相关文章
- 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 ...
随机推荐
- docker教程之从一头雾水到不一头雾水(3)
本文主要是介绍Docker容器的相关内容 容器创建 我们已经知道,镜像是只读的,而基于镜像创建出来的容器是可读写的,所以,一般我们实际中,会经常使用对应镜像创建容器并且使用这些容器.同样,如果我们想要 ...
- 初识C++之虚函数
1.什么是虚函数 在基类中用virtual关键字修饰.并在一个或多个派生类中被又一次定义的成员函数.使用方法格式为: virtual 函数返回类型 函数名(參数表) { 函数体 } 虚函数是实现多态性 ...
- Windows进程间通信--共享内存映射文件(FileMapping)--VS2012下发送和接收
之前以为两个互不相关的程序a.exe b.exe通信就只能通过网络,人家说可以通过发消息,我还深以为不然,对此,我表示万分惭愧. 之前课本上说的进程间通信,有共享内存.管道等之类的,但没有自己操刀写过 ...
- MySQL二:数据库操作
阅读目录 一 知识储备 二 初识SQL语言 三 系统数据库 四 创建数据库 五 数据库相关操作 一 知识储备 MySQL数据库基本操作知识储备 数据库服务器:一台计算机(对内存要求比较高) 数据库管理 ...
- lua学习笔记(三)
类型与值 lua是一种动态类型的语言,在语言中没有类型定义的语法,每个值都携带了它自身的类型信息 lua中有8种基础类型 nil 只与自身相等assert(nil==nil ...
- 检测session用户信息跳转首页界面
方案一:采用jsp方式检测用户信息跳转 <%@ page language="java" pageEncoding="UTF-8"%> <%@ ...
- win10 下eclipse tomcat 热部署问题?
前言: 问题的描述: 用的环境是maven,java,tomcat,win10 tomcat server配置如下 项目发布之后,修改jsp,报错,错误详情如下: 解决办法.勾选server opti ...
- iOS 生命周期 -init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 区别和用途
iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...
- poj1845(二分快速求等比数列模M和)
Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17039 Accepted: 4280 Descripti ...
- 记录-spring MultipartFile 文件上传
注意:以下上传和下载方法未必完全正确,不同浏览器效果不同,建议不要使用IE /** * 简单的文件上传 * @author:qiuchen * @createTime:2012-6-19 * @par ...