笔者的离线LCA模板。这里并查集和dfs的使用是个妙招。#include#include#include#define maxv 10005#define maxe 20005#define maxq 105using namespace std;struct edge{ int v,nxt;}e[...
分类:
其他好文 时间:
2015-12-30 09:12:08
阅读次数:
131
这道题不难,就是3个点的lca。算法有点多,写成树链剖分的吧!跑完2400多毫秒。还好,挺顺利的,加油!努力啊!注意看数据范围!相信自己,能行的 1 #include 2 #include 3 #include 4 #define rep(i,j,k) for(int i = j; i = k;.....
分类:
其他好文 时间:
2015-12-29 21:13:04
阅读次数:
222
题意:给出一个有n(n=2的除数的除法就会变为0。建树并生成LCA。每次处理第2种请求时,我们都是先求两点的LCA,再分别处理两点到其LCA的路径。对于从低到高走路径的过程中,连续的权值为1的边,我们用一种类似于并查集的方式将他们缩成一条边。例如:a是b的父亲,边权值为1。那么我们领link[b]=...
分类:
其他好文 时间:
2015-12-27 13:26:24
阅读次数:
238
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1787DescriptionInputOutputLCA水过三个点两两求LCA,若有其中两个LCA一样“集合点”就是第三个LCA多加inline有用处,DFS改BFS对效率没有什么提高从上到下...
分类:
其他好文 时间:
2015-12-26 15:08:27
阅读次数:
198
题目描述:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to thedefinition of LCA on Wikipe...
分类:
编程语言 时间:
2015-12-25 18:43:50
阅读次数:
140
题意:一个村子有n个房子,他们用n-1条路连接起来,每两个房子之间的距离为w。有m次询问,每次询问房子a,b之间的距离是多少。分析:近期公共祖先问题,建一棵树,求出每一点i到树根的距离d[i],每次询问a。b之间的距离=d[a]+d[b]-2*d[LCA(a,b)];LCA(a,b)是a,b的近期公...
分类:
编程语言 时间:
2015-12-22 13:06:25
阅读次数:
200
看了半天题解。。。。 http://hzwer.com/4019.html http://cxjyxx.me/?p=662 神犇一句“不用多说”蒟蒻弄了半天TAT。。。但实在懒(bu)得(gan)写链剖= = 接题解: 对于当前非树边(u,v),设t=lca(u,v),这条非树边可...
分类:
其他好文 时间:
2015-12-21 21:35:49
阅读次数:
262
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia:...
分类:
其他好文 时间:
2015-12-20 08:12:25
阅读次数:
158
链剖,居然还可以这样求LCA,学习了orz直接引用清华爷gconeice的题解吧显然,暴力求解的复杂度是无法承受的。考虑这样的一种暴力,我们把 z 到根上的点全部打标记,对于 l 到 r 之间的点,向上搜索到第一个有标记的点求出它的深度统计答案。观察到,深度其实就是上面有几个已标记了的点(包括自身)...
分类:
其他好文 时间:
2015-12-14 23:12:20
阅读次数:
326
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest...
分类:
其他好文 时间:
2015-12-12 16:55:35
阅读次数:
186