The Tree-planting Day and Simple Disjoint Sets

First I have to say: I have poor English. I am too young, too simple, sometimes na?ve.

It was tree-planting day two weeks ago. SHENBEN dph taught us a lot about tree-planting and the disjoint sets. It was useful and valuable for a JURUO like me. I admire all SHENBENs and orz all of them!

How to plant a tree?

First of all, you should know how to make "parent arrays". It is good, isn‘t it? Using an array f[] you can put information about someone‘s father. Use f[i], i is an element‘s index, and f[i] means the father‘s index.

And we can use disjoint sets now:

  1. value all elements in array f[] as the index itself. It means all elements‘ father are themselves, and they any of them is a single set.
  2. to union two sets, use f[find(y)] = find(x); code. This means one set "tree" is the father of another.
  3. to see if one and another are in a set, use if (find(x) == find(y)) to determine.

But how to union sets? You can regard this method as making a tree. We can link two trees into one tree, so the question of how many continuous blocks equals the question of how many trees.

And how to find one‘s daddy ancestor? Using DFS can help a lot. If A is the father of itself, it is the top ancestor. Or, it must we can DFS its father B then (we can make the top ancestor C we found the father of A. It can save time.

Disjoint-set data structure

So it‘s simple as these codes: (LUOGU P3367 Disjoint Sets)

 1 /* Luogu P3367 并查集
 2  * Au: GG
 3  */
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cmath>
 7 using namespace std;
 8 const int maxn = 10000 + 3;
 9 int n, m, z, x, y, f[maxn];
10 int find(int k) { // find father
11     return f[k] == k ? k : f[k] = find(f[k]);
12 }
13 int main() {
14     //freopen("p3367.in", "r", stdin);
15     scanf("%d%d", &n, &m);
16     while (n--) f[n] = n;
17     while (m--) {
18         scanf("%d%d%d", &z, &x, &y);
19         if (z == 1) {
20             f[find(y)] = find(x);
21         } else {
22             if (find(x) == find(y)) printf("Y\n");
23             else printf("N\n");
24         }
25     }
26     return 0;
27 }

Yes yes, it‘s quite simple at first. That‘s why we love mathematics computer science.

时间: 2024-10-06 07:32:29

The Tree-planting Day and Simple Disjoint Sets的相关文章

A Complete Tutorial on Tree Based Modeling from Scratch (in R &amp; Python)

A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python) MACHINE LEARNING PYTHON R SHARE  MANISH SARASWAT, APRIL 12, 2016 / 52 Introduction Tree based learning algorithms are considered to be one of the best and mostly used supervised

Aizu - ALDS1_7_C Tree Walk

Binary trees are defined recursively. A binary tree T is a structure defined on a finite set of nodes that either contains no nodes, or is composed of three disjoint sets of nodes:- a root node.- a binary tree called its left subtree.- a binary tree

Atitit。Tree文件解析器的原理流程与设计实现&#160;&#160;java&#160;&#160;c#&#160;php&#160;js

Atitit.Tree文件解析器的原理流程与设计实现  java  c# php js 1. 解析原理与流程1 1.1. 判断目录  ,表示服  dirFlagChar = "└├─";1 1.2. 剑豪制表符出现的位置与文件夹级别对应表1 1.3. 主要判读流程2 2. Tree结果2 3. Code----3 4. 结果5 1. 解析原理与流程 1.1. 判断目录  ,表示服  dirFlagChar = "└├─"; 其中-类似于剑豪的制表符是表示目录的..够

并查集 (Union-Find Sets)及其应用

定义 并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示. 集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的元素所在的集合合并. 主要操作 初始化 把每个点所在集合初始化为其自身. 通常来说,这个步骤在每次使用该数据结构时只需要执行一次,无论何种实现方式,时间复杂度均为O(N). 查找 查找元素所在的集合,即根节点. 合并 将两个元素所在的集合合并为一个集合. 通常来说,合并之前,应先判断两个元素是否属于

10+ 最流行的 jQuery Tree 菜单插件

jstree – jQuery Tree Plugin With HTML & JSON Data jstree is a lightweight and flexible jQuery plugin to create tree menu from HTML & JSON data sources sources.This jQuery Plugin Support AJAX & async callback loading.It is absolutely free, open

笔试算法题(38):并查集(Union-Find Sets)

出题:并查集(Union-Find Sets) 分析: 一种树型数据结构,用于处理不相交集合(Disjoint Sets)的合并以及查询:一开始让所有元素独立成树,也就是只有根节点的树:然后根据需要将关联的元素(树)进行合并:合并的方式仅仅是将一棵树最原始的节点的父亲索引指向另一棵树: 优化:加入一个rank数组存储节点深度的下界(从当前节点到其最远子节点的距离),从而可以启发式的对树进行合并,从而减少树的深度,防止树的退化:使 得包含较少节点的树根指向包含较多节点的树根,具体指代为树的高度:另

并查集(Disjoint Set)

在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中.这一类问题其特点是看似并不复杂,但数据量极大,若用正常的数据结构来描述的话,往往在空间上过大,计算机无法承受:即使在空间上勉强通过,运行的时间复杂度也极高,根本就不可能在规定的运行时间(1-3秒)内计算出试题需要的结果,只能用并查集来描述. 本文地址:http://www.cnblogs.com/archimedes/p/disj

数据结构之并查集Union-Find Sets

1.  概述 并查集(Disjoint set或者Union-find set)是一种树型的数据结构,常用于处理一些不相交集合(Disjoint Sets)的合并及查询问题. 2.  基本操作 并查集是一种非常简单的数据结构,它主要涉及两个基本操作,分别为: A. 合并两个不相交集合 B. 判断两个元素是否属于同一个集合 (1)       合并两个不相交集合(Union(x,y)) 合并操作很简单:先设置一个数组Father[x],表示x的"父亲"的编号.那么,合并两个不相交集合的方

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T