倍增求lca

LCA四种写法其一:倍增 文章目录众所周知一、倍增区间倍增二、LCA2.读入数据总结 众所周知 LCA有四种写法 一、倍增 fa[n][logn],fa[i][j]表示i节点的第2^j个父亲。 显然:fa[i][j]=fa[ fa[i][j-1] ][j-1] 即i的第2j个父亲是i的第2(j-1)个父亲的第2(j-1)个父亲。 于是,求i的第k个父亲的复杂度O(k)—>O(logk) 求i的第k个父亲: 1. for(int j=0; j<=log2(k); j++) 2. if((1<<j)&a. 阅读详情

倍增求lca 模板
求最近公共祖先lca(Least Common Ancestors),什么是公共祖先,给定一棵树,若节点z即使节点x的祖先,也是节点y的祖先,则称z是x,y的公共祖先,在x,y的所有公共祖先中,深度最大的节点称x,y的最近公共祖先,记做LCA(x,y)。
举个例子
在这里插入图片描述
LCA(4,5) = 2,LCA(5,6)=1,LCA(2,3)=1;
那么如何求LCA
先给出一张我们接下来要讲的例子:
例子
第一:暴力求法
先暴力dfs出每个点的深度,假设以 171717和 18 为例,既然要求LCA,那么我们就让他们一个一个向上跳,直到相遇为止。第一次相遇即是他们的LCA。 模拟一下就是:
17−>14−>10−>7−>3
18−>16−>12−>8−>5−>3
最终结果就是 3
但是这个算法的时间复杂度却是很高,所以我们采取了第二种重要的方法。
树上倍增法
设pre[x][k]表示x的2k 辈祖先,即从x向根节点走2k步到达的节点,若该节点不存在,则令pre[x][k] = 0,f[x][0]就是x的父节点。
所谓倍增,就是按2的倍数来增大,也就是跳 1,2,4,8,16,32 …… 不过在这我们不是按从小到大跳,而是从大向小跳,即按……32,16,8,4,2,1来跳,如果大的跳不过去,再把它调小。这是因为从小开始跳,可能会出现“悔棋”的现象。拿 5为例,从小向跳,5≠1+2+4,所以我们还要回溯一步,然后才能得出5=1+4;而从大向小跳,直接可以得出5=4+1。这也可以拿二进制为例,5(101),从高位向低位填很简单,如果填了这位之后比原数大了,那我就不填,这个过程是很好操作的。
还是以 17 和 18 为例(此例只演示倍增,并不是倍增LCA算法的真正路径):
17−>3
18−>5−>3
算法时间度为O(logn)。
接下来我们拆分算法步骤:
1:设depth[x]表示x的深度,不妨设depth[x]>=depth[y],否则交换x,y;
2:用上述二进制拆分思想,把x向上调整到与y同一高度。
3:若此时x==y,说明lca(x,y)就是x.
4:若此时x!=y,那么继续保持二进制拆分思想,把x,y同时向上调整,并保持深度一致并且二者不会相会。
/我们在跳的时候不能直接跳到它们的LCA,因为这可能会误判,比如4和8,在跳的时候,我们可能会认为1是它们的LCA,但1只是它们的祖先,它们的LCA其实是3。所以我们要跳到它们LCA的下面一层,比如4和8,我们就跳到4和5,然后输出它们的父节点,这样就不会误判了。/
5:此时x,y必定只差一步就会相会了,它们的父节点pre[x][0]就是我们要求的lca。

完整的求17和18的LCA的路径:
17−>10−>7−>3
18−>16−>8−>5−>3
解释:首先,18要跳到和17深度相同,然后18和17一起向上跳,一直跳到LCA的下一层(17是7,18是5),此时LCA就是它们的父亲

在算法实现的过程中,我们可以加上log2(x)log_2(x)log2​(x)的常数时间的优化。
下面给出各个步骤代码。
预处理:

void dfs(int f,int fa){
	depth[f] =  depth[fa] + 1;
	pre[f][0] = fa;
	for(int i=1;(1<<i) <= depth[f];i++)
		pre[f][i] = pre[pre[f][i-1]][i-1];//这个转移可以说是算法的核心之一
                                //意思是f的2^i祖先等于f的2^(i-1)祖先的2^(i-1)祖先
                                //2^i=2^(i-1)+2^(i-1)
	for(int i=head[f];i;i=nex[i]){
		if(ver[i] != fa)
		dfs(ver[i],f);
	}
}

查询x,y的lca:

int lca(int x,int y){
	if(depth[x] < depth[y]) swap(x,y); ////用数学语言来说就是:不妨设x的深度 >= y的深度
	while(depth[x] > depth[y])
	x = pre[x][lg[depth[x] - depth[y]]-1]; ////先跳到同一深度
	
	if(x==y) return x;//如果x是y的祖先,那他们的LCA肯定就是x了
	
	for(int i=lg[depth[x]]-1;i>=0;i--)  //不断向上跳(lg就是之前说的常数优化)
	if(pre[x][i]!=pre[y][i])  //因为我们要跳到它们LCA的下面一层,所以它们肯定不相等,如果不相等就跳过
	x = pre[x][i],y=pre[y][i];
	
	return pre[x][0]; 
}

给出测试链接:https://www.luogu.org/problem/P3379

给出模板:

#include<bits/stdc++.h>
using namespace std;
const int N = 5e5+7,M = 1e6+7;
int n,m,s,cnt;
int pre[N][22],depth[N],lg[N];
int head[M],ver[M],nex[M];
void add(int x,int y){
	ver[++cnt] = y;
	nex[cnt] = head[x];
	head[x] = cnt;
} 
void read(){
	scanf("%d%d%d",&n,&m,&s);
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
	}
}
void dfs(int f,int fa){
	depth[f] =  depth[fa] + 1;
	pre[f][0] = fa;
	for(int i=1;(1<<i) <= depth[f];i++)
		pre[f][i] = pre[pre[f][i-1]][i-1];//这个转移可以说是算法的核心之一
                                //意思是f的2^i祖先等于f的2^(i-1)祖先的2^(i-1)祖先
                                //2^i=2^(i-1)+2^(i-1)
	for(int i=head[f];i;i=nex[i]){
		if(ver[i] != fa)
		dfs(ver[i],f);
	}
}
int lca(int x,int y){
	if(depth[x] < depth[y]) swap(x,y); ////用数学语言来说就是:不妨设x的深度 >= y的深度
	while(depth[x] > depth[y])
	x = pre[x][lg[depth[x] - depth[y]]-1]; ////先跳到同一深度
	
	if(x==y) return x;//如果x是y的祖先,那他们的LCA肯定就是x了
	
	for(int i=lg[depth[x]]-1;i>=0;i--)  //不断向上跳(lg就是之前说的常数优化)
	if(pre[x][i]!=pre[y][i])  //因为我们要跳到它们LCA的下面一层,所以它们肯定不相等,如果不相等就跳过
	x = pre[x][i],y=pre[y][i];
	
	return pre[x][0]; 
}
void solve(){
	dfs(s,0);
	for(int i=1;i<=n;i++) ////预先算出log_2(i)+1的值,用的时候直接调用就可以了
	lg[i] = lg[i-1]+(1<<lg[i-1]==i);
	
	for(int i=1,x,y;i<=m;i++){
		scanf("%d%d",&x,&y);
		printf("%d\n",lca(x,y));
	}
}
int main(){
	read();
	solve();
	return 0;
}

资料参考:
https://www.luogu.org/blog/morslin/#
李煜东 算法竞赛进阶指南

倍增求LCA 求LCA的过程: 两个节点平层同时往上跳,直到相遇,相遇的点就是他们的LCA。但是很明显,如果这棵树的深度较大,那么就要跳很久了,所以可以采用倍增优化 设 F[x, k] 表示 x 的 第 2k 个祖先,即 x 节点向根节点走 2k 步到达的节点。f[x, 0] 就是 x 节点第一个祖先(也即父节点)。若该节点不存在(跳到了根节点外),我们则令 f[x, k] = 0 因为从x走2^k步, 可以分为先走2(k-1)步,再走2(k-1)步 得出F[x,k]的递推方程 ∀k∈[1,logn],F[x,k] 阅读详情

相关推荐

倍增算法入门 超详细解答+LCA+RMQ(ST表)+例题剖析

目录一、倍增算法二、倍增算法的应用:求LCA(最近公共祖先)附模板题三、倍增算法的应用:RMQ 问题(ST表)附模板题 一、倍增算法 要了解倍增之前,建议先看一下大佬的博客:【白话系列】倍增算法 看完以后相信你已经对倍增有了大致初步的了解,下面给出倍增的定义 (其实没什么用,好长一段话建议不看) 倍增 从字面的上意思看就是成倍的增长 ,这是指我们在进行递推时,如果状态空间很大,通常的线性递推无法满...

繁凡さん的博客 1万+

LCA问题(倍增法)

之前写过了LCA的两个在线算法之一:DFS+ST方法(DFS+ST)。本篇介绍另一种在线算法,用倍增思想来解决LCA问题。在学习倍增之前,先看一种倍增算法的退化版,有助于理解倍增法。 在找a和b的LCA时,先让a和b中深度较大的那一个,向上回溯到与另一个同样深度的位置上。例如下面这个图: 假设要找F和E的LCA,先让F向上回溯到他的父节点D的...

Q_M_X_D_D_的博客 1万+

C#海康威视摄像头控制,将IntPtr转换成Bitmap图片

C#通过skd控制海康威视摄像头,获取bitmap格式图片

ReflexMechanism的博客 6586

倍增LCA

朴素算法求LCA,即暴力求解,当树近乎为一条链时,找祖先时一级一级向上跳,时间复杂度接近O(n),所以可以考虑一次跳尽可能大的距离,即倍增算法;一次跳2^i(i=0,1,2....)级。 倍增法 一个节点的四级祖先就是这个节点的二级祖先的二级祖先; 设fa[j][i]为j节点的2^i级祖先;则fa[j][i]=fa[fa[j][i-1]][i-1]; 算法思想:求两节点的lca,先将深度深的结点用倍增法向上跳,直到两节点处于同一深度,此时有可能重合,则两点中任意一个结点即为lca;若没重合,则两节

qq_59144780的博客 4947

Vue3实战:从零构建ElementPlus后台管理系统全流程

本文详细介绍了从零开始使用Vue3和ElementPlus构建后台管理系统的全流程。内容涵盖环境准备、项目初始化、Vite配置、SCSS样式体系搭建、ElementPlus组件库引入与配置,以及封装可复用的SvgIcon组件进行图标管理,旨在帮助开发者快速搭建现代化、可维护的中后台应用。

weixin_29011239的博客 160

阿里巴巴达摩院人工智能训练师(高级)

阿里巴巴达摩院人工智能训练师(高级)

huang0612123的博客 1万+

倍增法在线求LCA(详解)

先贴上学习的文章: 洛谷P3379题解1 倍增讲解 在用Tarjan算法求LCA时会出现超时的现象,因为是一层一层的往上跳,于是就有了用倍增法求LCA,是层地往上跳。复杂度:O(nlogn) 1.倍增法思想: 按2的倍数跳,不过是从大到小跳,32,16,8,4,2,1。原因是从小到大跳会出现“悔棋”的现象: 从小到大:5!=1+2+4 从大到小:5=4+1 5<32 (不跳)...

m0_37579232的博客 1978

计算机组成原理汉字编码与校验设计实验报告(汉字国标码转区位码实验、汉字机内码获取实验、海明编码电路设计与海明解码)

这次实验利用logisim进行设计,进行了以下这四个实验,其中分为汉字国标码转区位码实验、汉字机内码获取实验、海明编码电路设计与海明解码,实验报告分为四个部分,在这四个部分中依次对四个实验的方案设计,设计思路进行了介绍

zhang2gongzi的博客 6万+

倍增求 LCA

LCA 指的是最近的公共祖先,倍增法求解 LCA 的步骤如下。 a. 求深度:对于每个节点 dfs 预处理处节点深度; b. 求倍增祖先:计算出每个节点向父元素跳 步所到达的节点(在这里 大于整棵树的最大深度) 备注: a. 设 f [ x , k ] 表示节点 x 向上跳 步能到达的祖先, f [ x , 0 ] 表示 x 的父元素,如对应节点不存在 f [ x , k ] = 0。 b. 将路径长度 分为两半,x 跳

Ryan_hsMax的博客 877

【AI知识点】启发式算法(Heuristic Algorithms)

启发式算法(Heuristic Algorithms) 是一种基于经验、直觉和局部优化的求解问题的算法,尤其适用于解决计算复杂度高、规模庞大的问题。它们通过简化问题、局部搜索和快速近似,提供了一种在短时间内获得“足够好”解的途径。虽然启发式算法无法保证全局最优解,但它们在实际应用中广泛使用,特别是在优化问题、任务调度和机器学习等领域。

AI完全体 3031

Langchain分割方式

【代码】Langchain分割方式。

2895

倍增 || ST-RMQ 求LCA

视频讲解戳这里(bj聚聚讲的非常好,一听就懂) 由luogu一道模板题讲起传送门(都9102年,这题还卡输入输出,要吸氧才能过) LCA:求两个点的最近公共祖先 倍增求LCA: 有很多博客图文并茂讲的非常清楚,倍增LCA的主要思想就是,先将u,v(保证u的深度更深)两点调整到同一水平线 情况一:u==v,那就说明最近公共祖先是u,这种情况说明u,v在同一边,祖先自然是深度较浅的那一个...

一只不争气的蒸汽机的博客 345

6、探索音乐结构中的统一性:贝多芬《英雄交响曲》第一乐章的深度剖析

本文深入剖析了贝多芬《英雄交响曲》第一乐章的音乐结构,探讨其统一性的来源及表现形式。通过主题与动机、和声与调性、旋律与节奏、织体与动态等多方面的分析,揭示了这部作品在技术层面和情感表达上的卓越成就。

b9c0d的博客 518

树上倍增法求 LCA

LCA(Lowest Common Ancestor,最近公共祖先)是树论中一个非常重要的概念,指的是在一棵树上两个节点最近的共同祖先节点。求解 LCA 的算法有很多,其中树上倍增法以其高效的查询效率(预处理 O (n log n),查询 O (log n))成为最常用的方法之一。本文将详细介绍树上倍增法的原理,并分享一份可直接复用的实现模板。

x3468714225的博客 595

河洛理数【陈抟】先天命卦大运

首先我们把前文提及的天干和地支进行上下划分,那么每一年的年份都是使用一天干和一地支进行组合,即如上的甲子、癸亥,那么下一年就是天干和地支都进行顺延一位进行组合,如甲子年下一年就是乙丑年,但是由于天干是十个、地支有十二个,所以要遍历一遍的话,就正好是 60 年。1924 年 ~ 1983 年(中元) 1984 年 ~ 2043 年(下元) 2044 年 ~ 2103 年(上元)2、找到卦中第一个(从下往上,也就是初爻、二到五爻、上爻)对应时辰的阴阳爻。5、也就是所属的元堂爻。

Daniel专栏 2071

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

倍增求LCA模板

#include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=5e5+10,maxk=1e4+10,lenOfNode=52274,INF=0x3f3f3f3f; int lg[maxn],n,m,d[maxn],f[maxn][21],dep[maxn],cnt; int nxt...

tgyy_cool的博客 202

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

V4yneの求lca的倍增模板

代码: const int maxn=1e5+50; vector<int> edge[maxn]; int rot;int dep[maxn]; int rec[maxn][30]; void dfs(int x,int fa) { rec[x][0]=fa; dep[x]=dep[fa]+1; for(int i=0;i<edge[x].size();i++) { ...

V4yne.的博客 385

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

树:倍增求LCA(最近公共祖先)

嘿,LCA的题目可是我最喜欢的题目了,今天我就来讲讲如何求LCA? 对于如何LCA,我只会几种,不过比较实用。我重点去讲倍增求LCA。 倍增求LCA 倍增?什么是倍增? 字面上的意思便是成倍增长,其实也就是ST表(不懂的上网搜一搜 ) 言归正传,那我们该怎么用倍增求LCA呢? 首先,我们设 fi,jf_{i,j}fi,j​ 表示从 iii 这个点向上跳 2j2^j2j 步所到达的点。 然后我们可以一遍dfs,处理出所有的 fi,0f_{i,0}fi,0​ 即每个点的父亲。 接着我们考虑如何转移: 首先我们先

LeeCongWei的博客 304

祖孙询问(倍增法求LCA)(全网最详细)(带图解)

倍增法求LCA的核心思路和步骤通过图片和实例理解起来并不复杂,代码写起来更是很顺畅。如果仍有不理解的地方,欢迎留言或者私信。

2301_77160836的博客 1503

倍增法求LCA模版

用倍增法求LCA,板子好打,不容易错。 #include #include #include #include #include using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define

fouzhe的博客 784

树上倍增求LCA

#include #include #include #include #include #include #include #include #include #include #define MAXN 500007 using namespace std; int N; int M; int S; int cnt; int f[MAXN]; int lv[MAXN]; int anc[MA

Angel_wing_SKY的博客 1056

倍增法求Lca——求大佬指点

良心倍增法求lca大佬写的:https://blog.youkuaiyun.com/wjh2622075127/article/details/81060586 首先说Lca,中文最近公共祖先,见名思意很容易知道是求树上两个点最近的公共祖先 第一种方法:两个点同时向上走,并把经过的点标记,这样当有一个点走到被标记的点的时候,这个点就是要找的点。 第二种方法:很明显第一种方法是最朴素的,下面来说优于第一种...

wearegamer的博客 228
上一篇: 1496:【例 3】架设电话线
下一篇: 1553:【例 2】暗的连锁 树上差分
A_Pathfinder
博客等级 码龄8年 29粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值