用树链剖分求LCA的模板: 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 const int maxn=40005; 5 int n,m; 6 int head[maxn],to[maxn<<1],w[maxn<<1],nxt[maxn<<1],tot; 7 int fa[maxn],dep[maxn],son[maxn],top[maxn],size[ma…
Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of…
Code: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; const int maxn=40000+3; typedef long long ll; ll dis[maxn]; int son[maxn],siz[maxn],rank1[maxn],p[maxn],top[maxn]; struct Edge { int…
For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. Now Kiki meets a very similar problem, kiki wants to design a container, the container is to support the three operations. Push: Push a given element e…
2020/4/30 15:55 树链剖分是一种十分实用的树的方法,用来处理LCA等祖先问题,以及对一棵树上的节点进行批量修改.权值和查询等有奇效. So, what is 树链剖分? 可以简单的理解为,将一棵树分成许多条不相交的链,每次我们只要得知链首,便可对该条链上所有的点用数据结构(like 线段树)进行相关操作 . 首先,介绍最常用的轻重链剖分. 明确最常用的轻重链概念: 重儿子:父亲节点的所有儿子中子树结点数目最多(size最大)的结点: 轻儿子:父亲节点中除了重儿子以外的儿子:…
洛谷上的lca模板题--传送门 学了求lca的tarjan算法(离线),在洛谷上做模板题,结果后三个点超时. 又把询问改成链式前向星,才ok. 这个博客,tarjan分析的很详细. 附代码-- #include <cstdio> #include <cstring> ; int n, m, cnt, s, cns; int x, y, z[maxn];//z是x和y的lca int f[maxn], head[maxn], from[maxn]; bool vis[maxn]; s…
HDU#4607. Park Visit 题目描述 Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visi…
Given a string, find the length of the longest substring T that contains at most k distinct characters. For example, Given s = “eceba” and k = 2, T is "ece" which its length is 3. 题意: 给定字符串,求至多包含K种字符的最长子串 思路: 跟[leetcode]159. Longest Substring wi…