coursera Algorithms week1 练习测验2:Union-find with specific canonical element
2017-07-17 22:57
375 查看
题目原文:
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.
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
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.
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
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 ; 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"); } }
相关文章推荐
- 并查集系列(二)——Union-find with specific canonical element
- Find Tables with Specific Field Names
- Find the element before and after a specific element
- Find document with array that contains a specific value
- 373. Find K Pairs with Smallest Sums && 378. Kth Smallest Element in a Sorted Matrix
- Find the k-th Smallest Element in the Union of Two Sorted Arrays
- Coursera Algorithms week1 练习测验3:Successor with delete
- give two sorted array, find the k-th smallest element of union of A and B
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- Invalid content was found starting with element 'taglib'”
- Cannot find source for binding with reference原因分析与解决方法 -[WPF开发]
- Android NDK: jni/Android.mk: Cannot find module with tag 'CocosDenshion/android' in import path
- cvc-elt.1: Cannot find the declaration of element 'beans'
- leetcode---Find Peak Element
- eclipse validator - Invalid content was found starting with element 'display-name' in web.xml
- 并查集(Union-Find)
- 并查集(Union-Find)算法介绍
- Selenium Xpath Tutorials - Identifying xpath for element with examples to use in selenium
- Codeforces 371D (Union-find)
- ibatis Could not find SQL statement to include with refid 'xxx' 问题