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)

 /* Luogu P3367 并查集
* Au: GG
*/
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = + ;
int n, m, z, x, y, f[maxn];
int find(int k) { // find father
return f[k] == k ? k : f[k] = find(f[k]);
}
int main() {
//freopen("p3367.in", "r", stdin);
scanf("%d%d", &n, &m);
while (n--) f[n] = n;
while (m--) {
scanf("%d%d%d", &z, &x, &y);
if (z == ) {
f[find(y)] = find(x);
} else {
if (find(x) == find(y)) printf("Y\n");
else printf("N\n");
}
}
return ;
}

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

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

  1. Disjoint Sets

    Disjoint Sets Disjoint Sets的意思是一堆集合們,它們相互之間都沒有交集.沒有交集是指:各個集合之間沒有擁有共同.相同的元素.中文稱作「分離集」. Disjoint Sets的 ...

  2. 算法实践--不相交集合(Disjoint Sets)

    什么是不相交集合(Disjoint Sets) 是这样的一组set,任何元素最多只能在一个set中 至少支持查找Find和合并Union操作 实现方式(基于树) 每个set都是一棵树 每棵树都由树的根 ...

  3. [hdu6984]Tree Planting

    构造一个01矩阵,其中格子$(i,j)$​​​​​对应于第$ik+j$​​个​​​的位置(其中$0\le i<\lceil\frac{n}{k}\rceil,0\le j<k$​​​,位置 ...

  4. HDU 6984 - Tree Planting(数据分治+状压 dp)

    题面传送门 傻逼卡常屑题/bs/bs,大概现场过得人比较少的原因就是它比较卡常罢(Fog 首先对于这样的题我们很难直接维护,不过注意到这个 \(n=300\) 给得很灵性,\(k\) 比较小和 \(k ...

  5. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  6. A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python)

    A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python) MACHINE LEARNING PYTHON  ...

  7. Linux and the Device Tree

    来之\kernel\Documentation\devicetree\usage-model.txt Linux and the Device Tree ----------------------- ...

  8. 数据结构与算法分析 – Disjoint Set(并查集)

    什么是并查集?并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题. 并查集的主要操作1.合并两个不相交集合2.判断两个元素是否属于同一集合 主要操作的解释 ...

  9. 并查集(Disjoint Set)

    在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中.这一类问题其特点是看似并不复杂, ...

随机推荐

  1. 屏幕适配dip

    android适配一般使用dpi 那dpi与分辨率,屏幕尺寸的关系 DPI值计算是屏幕对角线的像素值除以屏幕的大小 dip=/ 屏幕尺寸, 比如:计算WVGA(800*480)分辨率,3.7英寸的密度 ...

  2. Xmanger远程连接Centos7(成功配置)

    参考1:https://blog.csdn.net/wuzhimang/article/details/51523867 参考2:https://blog.csdn.net/name_kongkong ...

  3. hdu6576Worker(最小公倍数)

    Problem Description Avin meets a rich customer today. He will earn 1 million dollars if he can solve ...

  4. Java程序流程控制

    程序流程控制有 选择,循环,以及跳转结构 选择结构中无非就是 If 和 switch语句我两种都做了一些小案例 1. 利用Scanner库来获得控制台用户输入的数字与代码中定义的变量比较 packag ...

  5. [NOIP2016PJ]魔法阵

    今天模拟赛的题,,,唯一没有Giao出来的题(不然我就AKIOI了~) 最开始没想到数学题,把所有部分分都说一遍吧: 35分:纯暴力O(M^4)枚举,对于每一组a,b,c,d验证其是否合法. 60分: ...

  6. Windows组决策

    https://blog.csdn.net/wangjunjun2008/article/details/82426587

  7. C#中XmlTextWriter读写xml文件详细介绍

    XmlTextWriter类允许你将XML写到一个文件中去.这个类包含了很多方法和属性,使用这些属性和方法可以使你更容易地处理XML.为了使用这个类,你必须首先创建一个新的XmlTextWriter对 ...

  8. 在pythonanywhere部署你的第一个应用

    pythonanywhere是一个免费的托管python的代码,可以测试你的web应用,用起来还是比较方便的,现在就来介绍如何在pythonanywhere部署你的应用. 下载你的代码 我的代码是托管 ...

  9. JavaScript——正则匹配、正则提取、正则替换

    正则匹配 // 匹配日期 var dateStr = '2015-10-10'; var reg = /^\d{4}-\d{1,2}-\d{1,2}$/ console.log(reg.test(da ...

  10. 关于Object.create方法

    ES6最新的Object.create语法是 创造一个对象 可以传参,参数为一个对象,得到的结果是一个克隆的对象, 实际上 这是基于原型的克隆 分析如下: var a={b:1}; var a1 = ...