
数据结构---树
破晓aa
这个作者很懒,什么都没留下…
展开
-
LCA(最近公共祖先)Tarjan算法模板
可以通过 poj 1330 #include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;/**1.dfs2.并査集3.邻接表(vector、数组模拟)*/vector < int &g...原创 2018-10-18 20:06:33 · 218 阅读 · 0 评论 -
B - Boatherds POJ - 2114 (树上点分治)
Boatherds Inc. is a sailing company operating in the country of Trabantustan and offering boat trips on Trabantian rivers. All the rivers originate somewhere in the mountains and on their way down to ...原创 2019-01-08 14:54:45 · 223 阅读 · 0 评论 -
G - 聪聪可可 HYSBZ - 2152 (点分治)
聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃、两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已经玩儿腻了这种低智商的游戏。他们的爸爸快被他们的争吵烦死了,所以他发明了一个新游戏:由爸爸在纸上画n个“点”,并用n-1条“边”把这n个“点”恰好连通(其实这就是一棵树)。并且每条“边”上都有一个数。接下来由聪...原创 2019-01-08 10:42:15 · 152 阅读 · 0 评论 -
Codeforces Round #530 (Div. 2) D. Sum in the tree
Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root has index 11. Each vertex vv initially had an integer number av≥0av≥0 written on it. For every vertex vv Mitya has comput...原创 2019-01-07 16:28:57 · 578 阅读 · 0 评论 -
Tree POJ - 1741 (点分治模板)
Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is cal...原创 2019-01-04 21:32:24 · 255 阅读 · 0 评论 -
树的重心
树的重心:对于一颗n个节点的无根树,找到一个点,使得把树变成以该节点为根的有根树时,最大子树的节点数最小,也就是说删除这个点后最大联通块的节点数最小(引自刘汝佳的算法竞赛入门经典)具体要怎么求呢?其实求树的重心算是一个比较基础的树形dp。用dp[i]代表以i为根的子树的所有节点个数(包含i节点本身),如果要以i为重心的话,那么其最大子树的节点数就是max(max(dp[j]), n - dp[...原创 2019-01-04 10:56:10 · 2193 阅读 · 0 评论 -
树的直径
树的直径:对于一颗无根树,找到两个点,使得他们之间的距离最远,这个距离就是树的直径。求解方法:1.两次dfs(或者bfs)先随便找一个节点a作为根,以该节点为起点遍历树找到和这个节点距离最远的节点b,然后以b为起点,找到和b距离最远的节点c,b和c之间的距离即为树的直径。代码:#include <iostream>#include <cstdio>...原创 2019-01-04 08:46:57 · 487 阅读 · 0 评论 -
字典树(Trie树)模板 数组表示 + 链表表示
数组模拟,缺点是并不知道要开多大,可能会出现数组开小导致wrong answer。对应题目:hdu 1251 #include <iostream>#include <cstdio>#include <cstring>#define fuck cout << "wtf???\n"using namespace std;type...原创 2018-11-21 21:20:52 · 891 阅读 · 0 评论 -
伸展树(Splay)(转载)
附上原文地址:https://www.cnblogs.com/yousiki/p/6147455.html经典引文 空间效率:O(n)时间效率:O(log n)插入、查找、删除创造者:Daniel Sleator 和 Robert Tarjan优点:每次查询会调整树的结构,使被查询频率高的条目更靠近树根。 Tree Rotation 树的旋转是sp...转载 2018-10-24 16:59:42 · 226 阅读 · 0 评论 -
Tree POJ - 3237 (树链剖分)
思路:同时存下最大值和最小值,每次该区间数据元素乘-1就把最大值变最小值 就ok了#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define Fuck cout << "wtf?????\n"using names...原创 2018-10-23 17:18:41 · 147 阅读 · 0 评论 -
Query on a tree SPOJ - QTREE(树链剖分 + 线段树(区间更新 + 单点查询,存的是边权))
#include &amp;lt;iostream&amp;gt;#include &amp;lt;cstdio&amp;gt;#include &amp;lt;cstring&amp;gt;using namespace std;const int maxn = 2e4+100;int son[maxn], deep[maxn], father[maxn], num[maxn],原创 2018-10-20 15:06:44 · 175 阅读 · 0 评论 -
树链剖分(区间更新,单点查询,存的是点权)Aragorn's Story HDU - 3966
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define fuck cout << "wtf?????\n"using namespace std;const int maxn = 50009;int n, m...原创 2018-10-22 20:08:29 · 236 阅读 · 0 评论 -
Educational Codeforces Round 58 D. GCD Countin(分解质因子+树形dp)
题目链接题意:给出一棵树,每个节点有权值,g(x, y)代表x节点到y节点路径上的最大公因子,dist(x, y)代表x到y的距离,求在g(x, y) > 1的条件下,dist(x, y)的最大值思路:首先要明白,要使gcd(x, y) > 1,只需要二者有公共质因子就行了,所以必须要分解质因子了,而且可以知道每个数的质因子不超过7个(因为前七个素数之积已经超过1e5了)...原创 2019-01-14 10:36:37 · 146 阅读 · 0 评论