题目原文:

Add a method find() to the union-find data type so that find(i) returns the largest element in the connected component containing i. The operations, union(), connected(), and find() should all take logarithmic time or better.

 import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class FindLargestUF {
private int[] id;
private int count;
public FindLargestUF(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));
}
public int find(int p) {
while (p != id[p])
p = id[p];
return p;
} public void union(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
StdOut.println("find("+p+")="+pRoot+",find("+q+")="+qRoot);
if (pRoot == qRoot)
return;
else if (pRoot < qRoot)
id[pRoot] = qRoot;
else
id[qRoot] = pRoot;
count--;
} public static void main(String[] args) {
int n = StdIn.readInt();
FindLargestUF uf = new FindLargestUF(n);
while (!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt();
if (uf.connected(p, q))
continue;
uf.union(p, q);
StdOut.println("link points:" + p + " " + q);
}
StdOut.println(uf.count() + "components");
}
}

For example, if one of the connected components is {1,2,6,9}, then the find() method should return 9 for each of the four elements in the connected components.

分析:

这一题很简单,要求find到的根是子集中的最大元素。因此只需要在union时,用两个子集中较大的root作为合并后的root就可以了。以下代码提交100

Coursera Algorithms week1 查并集 练习测验:2 Union-find with specific canonical element的更多相关文章

  1. Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity

    题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...

  2. Coursera Algorithms week1 查并集 练习测验:3 Successor with delete

    题目原文: Given a set of n integers S = {0,1,…,N-1}and a sequence of requests of the following form: Rem ...

  3. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  4. Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time

    题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...

  5. Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法

    第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...

  6. Coursera Algorithms week2 基础排序 练习测验: Permutation

    题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...

  7. Coursera Algorithms week2 基础排序 练习测验: Intersection of two sets

    题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subq ...

  8. Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space

    题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...

  9. Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST

    题目原文: Given a binary tree where each 

随机推荐

  1. java攻城师之路--复习java web之Cookie_Session

    Servlet技术 用来动态生成 网页数据资源Servlet生成HTML 页面数据时,所有内容都是通过 response.getWriter response.getOutputStream 向浏览器 ...

  2. 学习java编程能往哪些方向发展

    当下Java训练非常热,是因为通过学习java能够快速的就业,这对于今年就业压力非常大的大学生来说,无疑是一条就业的捷路,虽然培教育费动辄过万,但还是非常值得的. 可是你可曾想过,学习了java编程后 ...

  3. async await 同步方法调用异步方法死锁

    同步方法调用异步方法.GetAwaiter().GetResult()计算函数超时,异步方法所有的回调操作都会期望返回到主线程. 所以会导致各种线程死锁.异步方法中使用ConfigureAwait(f ...

  4. Docker是什么?可以用Docker做什么?

    作者:刘允鹏 链接:https://www.zhihu.com/question/28300645/answer/67707287 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  5. ubuntu 更改终端颜色

    1.$ sudo gedit .bashrc 2.PS1="\[\033[1;36;01m\]\u\[\033[00m\]\[\033[1;34;01m\]@\[\033[00m\]\[\0 ...

  6. Python中的@property装饰器

    要了解@property的用途,首先要了解如何创建一个属性. 一般而言,属性都通过__init__方法创建,比如: class Student(object): def __init__(self,n ...

  7. Spider-Python实战之通过Python爬虫爬取图片制作Win7跑车主题

    1. 前期准备 1.1 开发工具 Python 3.6 Pycharm Pro 2017.3.2 Text文本 1.2 Python库 requests re urllib 如果没有这些Python库 ...

  8. SqlServer转换为Mysql(mss2sql)

    SqlServer转换为Mysql(mss2sql)工具 http://pan.baidu.com/s/1c2d8R8O 参考链接: http://www.cnblogs.com/angestudy/ ...

  9. 关于PyQt5,在pycharm上的安装步骤及使用技巧

    前序 之前学习了一款GUI图形界面设计的Tkinter库,但是经大佬的介绍,PyQT5全宇宙最强,一脸的苦笑 毫不犹豫的选择转战PyQT5,在学习之前需要先安装一些必须程序,在一番查阅后,发现PyQt ...

  10. [COGS 2264]魔法传输

    [COGS 2264]魔法传输 题目 自从看了<哈利波特>,小Y就十分渴望获得魔法值.于是他和一群向往魔法的孩子(当然这些孩子们都是不会魔法的)来到了哈利波特的家,大家坐成一排.哈利波特会 ...