three version are provided.

disjoint set, linked list version with weighted-union heuristic, rooted tree version with rank by union and path compression, and a minor but substantial optimization for path compression version FindSet to avoid redundancy so to be more efficient. (31 ms to 15 ms)

reference:

1. Thomas H. Cormen, Introduction to Algorithms

2. Disjoint-set Data Structures By vlad_D– TopCoder Member https://www.topcoder.com/community/data-science/data-science-tutorials/disjoint-set-data-structures/

in linked list version with weighted-union heuristic, with a extra tail member in struct myNode to speedup union, find is O(1), simply the p->head, so I remove find() and just used p->head as the find function.

(one main point) every time a list become no longer list, change its head’s num from 1 to 0, thus facilitate the afterwards count process – all node’s num is simply 0 except the ones as the head of linked lists. – similar process in the rooted tree version.

// linked list version with weighted-union heuristic, 15 ms

#include <cstdio>
#include <cstring>
#include <algorithm> #define MAXSIZE 1005
struct myNode {
int num;
myNode *head;
myNode *next;
myNode *tail;
}; void MergeSet(myNode *p1, myNode *p2) {
p1=p1->head, p2=p2->head;
if(p1->num<p2->num) { std::swap(p1,p2); }
p1->num+=p2->num, p2->num=0;
p1->tail->next=p2, p1->tail=p2->tail;
for(;p2;p2=p2->next) { p2->head=p1; }
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T, n,m,u,v,i,cnt;
myNode cities[MAXSIZE], *p,*pend, *q;
while(scanf("%d%d",&n,&m)==2 && n>0) {
for(p=&cities[1],pend=p+n;p!=pend;++p) { p->num=1; p->head=p; p->next=0; p->tail=p; }
for(i=0;i<m;++i) {
scanf("%d%d",&u,&v);
if(cities[u].head!=cities[v].head) { MergeSet(&cities[u],&cities[v]); }
}
for(cnt=-1, p=&cities[1],pend=p+n;p!=pend;++p) {
if(p->num>0) ++cnt;
}
printf("%d\n",cnt);
}
return 0;
}

// rooted tree version, with rank by union and path compression, 31 ms

#include <cstdio>
#include <cstring>
#include <algorithm> #define MAXSIZE 1005
struct myNode {
int rank;
myNode *parent;
}; myNode* FindSet(myNode *p1) {
if(p1->parent==p1) return p1;
return p1->parent=FindSet(p1->parent);
} void Link(myNode *p1, myNode *p2) {
if(p1->rank<p2->rank) std::swap(p1,p2);
p2->parent=p1;
p2->rank=0;
if(p1->rank==p2->rank) ++p1->rank;
} void MergeSet(myNode *p1, myNode *p2) {
Link(FindSet(p1),FindSet(p2));
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T, n,m,u,v,i,cnt;
myNode cities[MAXSIZE], *p,*pend, *q;
while(scanf("%d%d",&n,&m)==2 && n>0) {
for(p=&cities[1],pend=p+n;p!=pend;++p) { p->rank=1; p->parent=p; }
for(i=0;i<m;++i) {
scanf("%d%d",&u,&v);
if(FindSet(&cities[u])!=FindSet(&cities[v]))
MergeSet(&cities[u],&cities[v]);
}
for(cnt=-1, p=&cities[1],pend=p+n;p!=pend;++p) {
if(p->rank>0) ++cnt;
}
printf("%d\n",cnt);
}
return 0;
}

note that in version 2, the path compression FindSet is a two pass recursive function, first pass up to find parent and then pass down return it to update p->parent, thus achieve path compression.

(another main point) But, note that, even when p->parent is the representative, there is no need to update p->parent, FindSet still obliviously call FindSet(p->parent) and assign it to p->parent, which does nothing useful. We can remove this redundancy by a simple modification, in FindSet, replace

if(p1->parent==p1) return p1;

with

if(p1->parent==p1->parent->parent) return p1->parent;

// almost same with version 2, with the optimization just mentioned, 15 ms

#include <cstdio>
#include <cstring>
#include <algorithm> #define MAXSIZE 1005
struct myNode {
int rank;
myNode *parent;
}; myNode* FindSet(myNode *p1) {
if(p1->parent==p1->parent->parent) return p1->parent;
return p1->parent=FindSet(p1->parent);
} void Link(myNode *p1, myNode *p2) {
if(p1->rank<p2->rank) std::swap(p1,p2);
p2->parent=p1;
p2->rank=0;
if(p1->rank==p2->rank) ++p1->rank;
} void MergeSet(myNode *p1, myNode *p2) {
Link(FindSet(p1),FindSet(p2));
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int T, n,m,u,v,i,cnt;
myNode cities[MAXSIZE], *p,*pend, *q;
while(scanf("%d%d",&n,&m)==2 && n>0) {
for(p=&cities[1],pend=p+n;p!=pend;++p) { p->rank=1; p->parent=p; }
for(i=0;i<m;++i) {
scanf("%d%d",&u,&v);
if(FindSet(&cities[u])!=FindSet(&cities[v]))
MergeSet(&cities[u],&cities[v]);
}
for(cnt=-1, p=&cities[1],pend=p+n;p!=pend;++p) {
if(p->rank>0) ++cnt;
}
printf("%d\n",cnt);
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.

hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏的更多相关文章

  1. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  2. hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏

    partial sort. first use std::nth_element to find pivot, then use std::stable_partition with the pivo ...

  3. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. [leetcode] Reverse Linked List 分类: leetcode 算法 2015-07-09 18:44 2人阅读 评论(0) 收藏

    反转链表:比较简单的问题,可以遍历也可以递归. # Definition for singly-linked list. class ListNode: def __init__(self, x): ...

  6. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  7. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  8. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  9. hdu 1052 (greedy algorithm) 分类: hdoj 2015-06-18 16:49 35人阅读 评论(0) 收藏

    thanks to http://acm.hdu.edu.cn/discuss/problem/post/reply.php?action=support&postid=19638&m ...

随机推荐

  1. IOS7 edgesForExtendedLayout

    在iOS 7中,苹果引入了一个新的属性,叫做[UIViewController setEdgesForExtendedLayout:],它的默认值为UIRectEdgeAll.当你的容器是naviga ...

  2. 【网摘】CURL常用命令

    原文地址: http://www.thegeekstuff.com/2012/04/curl-examples/ 下载单个文件,默认将输出打印到标准输出中(STDOUT)中 curl http://w ...

  3. Chrome谷歌浏览器下不支持css字体小于12px的解决办法

    先来看下 ie.火狐.谷歌浏览器下各个字体显示情况 ie下: 火狐下: 谷歌下: 从上面的图可以很明显看出谷歌下 css设置字体大小为12px及以下时,显示都是一样大小,都是默认12px; 那么网上一 ...

  4. NFC

    NFC手机是指带有NFC模块的手机.带有NFC模块的手机可以做很多相应的应用.NFC是Near Field Communication缩写,即近距离无线通讯技术.在13.56MHz频率运行于20厘米距 ...

  5. struts 头像上传

    java代码: 1 package cn.itcast.nsfw.user.action; import java.io.File; import java.io.IOException; impor ...

  6. jQuery Mobile学习日记之HelloWorld

    这里是本人学习jQuery Mobile的过程,主要用于记录,过程中有不对的地方或不严谨的地方,欢予以指出纠正,非常感谢! 1.首先是下载安装以下文件: [Opera Mobile Emulator] ...

  7. [HTML/HTML5]3 页面结构

    在HTML5之前,主要的容器元素是div元素,但在HTML5中提供了数种其它容器元素供我们使用. 因此,当组织Web页面结构时,首先使用HTML将内容分成多部分,然后在对其使用CSS应用样式和格式. ...

  8. 制作简单的2D物理引擎(零)

    最近发现了Github上的开源物理引擎项目Matter.js,对它很感兴趣,发现源码并不算长,算上注释大约1万行左右,值得剖析一番.Matter.js实现一个最小化的2D物理引擎,性能不错,故打算用C ...

  9. AutoCAD安装失败

    问题一: Installing .NET Framework Runtime 4.0: D:\安装包\CAD\cad2012(x64)\Map3D2012(x64)\3rdParty\NET\4\wc ...

  10. android事件拦截处理机制详解

    前段时间刚接触过Android手机开发,对它的事件传播机制不是很了解,虽然网上也查了相关的资料,但是总觉得理解模模糊糊,似是而非,于是自己就写个小demo测试了一下.总算搞明白了它的具体机制.写下自己 ...