QuickFind-【Princeton-Robert Sedgewick】算法
来源:互联网 发布:linux jre 环境变量 编辑:程序博客网 时间:2024/06/06 07:14
代码基本是照敲的,测试QuickFind算法
<span style="font-size:14px;">package quickFind;import java.util.Scanner;public class QF {private int[] id;public QF(int N){id = new int[N];for(int i = 0; i < N; i++){id[i] = i;//set id of each object to itself}}public boolean connected(int p, int q)//check whether p and q are in the same component{return id[p] == id[q];}public void union(int p, int q)//change all entries with id[p] to id[q]{int pid = id[p];int qid = id[q];for(int i = 0; i < id.length; i++)if(id[i] == pid) id[i] = qid;}public static void main(String args[]){Scanner s = new Scanner(System.in);int N = s.nextInt();QF qf = new QF(N);int count = 0;while(++count < N){int p = s.nextInt();int q = s.nextInt();if(!qf.connected(p, q)){qf.union(p, q);System.out.println(p + " " + q);}}for(int i = 0; i < N; i++)System.out.print(qf.id[i] + " ");System.out.println();s.close();}}</span>
0 0
- QuickFind-【Princeton-Robert Sedgewick】算法
- 算法 Robert Sedgewick 习题答案 1.1 基础编程模型
- 算法 Robert Sedgewick 习题答案 1.2 数据抽象
- 最好的算法讲解视频:Algorithms by Robert Sedgewick
- 算法、数据结构经典资料简介(TAOCP、Robert Sedgewick、算法导论、编程珠玑)
- 读《图算法,Robert Sedgewick》笔记 —— 最短路径
- 算法(第四版 Robert Sedgewick)课后练习答案 1.1 基础编程模型
- 拜读Robert Sedgewick的《算法 第4版》——1.1基础编程模型
- 《算法:C语言实现(第1-4部分)基础知识、数据结构、排序及搜索(原书第3版)》┊Robert Sedgewick[.PDF]
- coursera算法笔记:QuickFind中union隐蔽错误
- Princeton-普林斯顿-算法-第一题-Percolation
- Sedgewick之巨著《算法》,与高德纳TAOCP一脉相承
- Sedgewick之巨著《算法》,与高德纳TAOCP一脉相承
- 算法 Robert 第四版 准备篇
- 【Java、算法】Princeton Algorithms Part I Week 1 Exercise
- [4] 算法之路 - 插入排序之Shell间隔与Sedgewick间隔
- Algorithm, Part I Princeton University 公开课第一周 Union-Find算法
- Robert Hanek
- CentOS 6.5 64位安装JDK、tomcat
- 创建 iOS 8 Today extension and share data with containing app – tutorial
- Webdriver - Reset Safari
- 3d模型 缩放影响渲染效率
- (一)洞悉linux下的Netfilter&iptables:什么是Netfilter?
- QuickFind-【Princeton-Robert Sedgewick】算法
- 车辆违章查询演示示例
- let's go!
- 安装Linux系统磁盘分配方案
- Meanshift解析
- 原码, 反码, 补码 详解
- web移动端开发(1)
- asp.net文件读取和修改保存练习
- Target failed to run: Permission to debug *** was denied.