LCA问题的Tarjan离线算法 + POJ 1470

YOLOv8+BiFPN双向特征金字塔:多尺度目标检测精度提升28%的实战教程 双向特征金字塔网络(BiFPN)是一种强化多尺度特征融合支持“自顶向下+自底向上”的双向特征流动,同时保留不同尺度的细节信息;引入加权融合机制,自动学习不同特征的重要性权重;裁剪冗余节点,在轻量化的同时提升融合效率。 阅读详情
树的最近公共祖先(Lowest Common Ancestor)问题是树结构上最经典的问题之一。
给一棵树T,每个询问形如:“点u和点v 的公共祖先是哪个点?”,此问题的答案被记为LCA(u, v)。LCA问题的算法分为在线和离线(Tarjan算法)两种,前者要求在回答后一个问题之前必须给出前一个问题的输出,而离线问题允许在读入所有询问之后一次性给出所有问题的答案。

LCA问题的应用很多,例如它可以用来回答这样的询问:“点u和点v的距离是多少?”——由于在树中两点的简单路是唯一的,所以这个距离等于u到LCA(u, v)再到v的距离,关键仍然是LCA。



Tarjan离线算法是基于树的dfs和并查集的,我们令x=LCA(u, v),用这么一句话解释这个算法:如果我们在访问完节点u及其后代后,将u的祖先指向为x,那么在访问x的其它后代节点v时,LCA(u,v)就为u的(当前的)祖先点x

int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}//x的祖先节点

void tarjan(int u)//在main中调用tarjan(root)
{
	vis[u] = true;
	int i, v;
	for (i = 1; i <= query[u][0]; ++i)//query[u][0]表示形如LCA(u,?)的查询个数
	{
		v = query[u][i];
		if (vis[v]) /*printf LCA(u,v)=find(v) or others*/
	}
	for (i = 1; i <= son[u][0]; ++i)//son[u][0]表示u的儿子个数
	{
		v = son[u][i];
		if (!vis[v]) tarjan(v), fa[v] = u;
	}
}


例题:

POJ 1470 Closest Common Ancestors  

此题样例的函数调用过程如下:

tarjan(5)
	
	tarjan(1)
		++sum[find(5)->5];
	//1 done
	fa[1]=5;
	
	tarjan(4)
		++sum[find(1)->find(5)->5];
	//4 done
	fa[4]=5;
	
	tarjan(2)
		++sum[find(4)->find(5)->5];
		tarjan(3)
			++sum[find(2)->2];//注意此时fa[2]尚未指向5
			++sum[find(1)->find(5)->5];
			++sum[find(4)->find(5)->5];
		//3 done
		fa[3]=2;
	//2 done
	fa[2]=5;
	
//5 done

代码实现:

/*375ms,6780KB*/

#include<cstdio>
#include<cstring>
const int mx = 905;

int sum[mx], son[mx][mx], query[mx][mx], fa[mx];
bool vis[mx], hasroot[mx];

int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);}

void tarjan(int u)
{
	vis[u] = true;
	int i, v;
	for (i = 1; i <= query[u][0]; ++i)
	{
		v = query[u][i];///要想使sum增加,v必须是先前访问过的点
		if (vis[v]) ++sum[find(v)];
	}
	for (i = 1; i <= son[u][0]; ++i)
	{
		v = son[u][i];
		if (!vis[v]) tarjan(v), fa[v] = u;
	}
}

inline void add(int a, int b)
{
	++query[a][0];///我们用query[节点a][0]表示查询LCA(a,?)的个数
	query[a][query[a][0]] = b;///记录这一查询LCA(a,b)
}

int main()
{
	int n, i, j, k, Q;
	while (~scanf("%d", &n))
	{
		memset(vis, 0, sizeof(vis));
		memset(sum, 0, sizeof(sum));
		memset(hasroot, 0, sizeof(hasroot));
		for (i = 1; i <= n; ++i)
		{
			fa[i] = i, query[i][0] = 0;
			scanf("%d", &j);
			while (getchar() != '(');
			scanf("%d", &son[j][0]);///注意,我们用son[节点编号][0]表示儿子个数
			while (getchar() != ')');
			for (k = 1; k <= son[j][0]; ++k)
			{
				scanf("%d", &son[j][k]);///记录此节点的儿子节点编号
				hasroot[son[j][k]] = true;
			}
		}
		scanf("%d", &Q);
		while (Q--)
		{
			while (getchar() != '(');
			scanf("%d%d", &i, &j);
			while (getchar() != ')');
			add(i, j), add(j, i);///双向添加查询
		}
		for (i = 1; i <= n; ++i)
			if (!hasroot[i]) {tarjan(i); break;}///从根节点进入
		for (i = 1; i <= n; ++i)
			if (sum[i]) printf("%d:%d\n", i, sum[i]);
	}
	return 0;
}

ABAP BDC 处理踩坑笔记:把错误挡在录制之前,把稳定做进每一行 BDCDATA 本文总结了SAP BDC(批输入)开发中的常见问题和解决方案。重点包括:1)理解BDCDATA本质是模拟人工操作而非直接操作数据库;2)数据格式化技巧,如金额、日期、物料号等字段的标准化处理;3)通过CTU_PARAMS参数控制执行环境,确保屏幕尺寸和更新模式一致;4)不要依赖sy-subrc,应通过解析MESSTAB消息判断业务是否成功;5)录制阶段的常见陷阱及解决方法;6)疑难问题的系统性解决思路;7)建议建立完善的错误处理机制,包括日志记录和重跑功能。文章提供了实用的ABAP代码示例和配置建议,帮助开 阅读详情

相关推荐

win10 运行lisflood_8踩过的坑

1、安装cuda及VS2019 https://www.bilibili.com/read/cv7335974/ 2、netcdf.h头文件找不到问题 https://www.cnblogs.com/lyggqm/p/12926974.html https://blog.youkuaiyun.com/u011954296/article/details/54617048 3、编码问题 https://blog.youkuaiyun.com/qq_33485434/article/details/81196843 4、

我的笔记 4115

LCA离线算法

#include #include #include #include using namespace std; vector c[10005]; bool visit[10005]; int n,p[10005],U,V; int Find(int x) { return x==p[x]?x:p[x]=Find(p[x]); } void dfs(int root,int fa) {

blog 274

洪水风险图制作全流程:HEC-RAS 与 ArcGIS 的耦合应用

本文系统介绍HEC-RAS水动力模拟软件在水利工程中的应用技术。内容涵盖:1)软件理论与安装;2)一维地形数据导入与河网构建;3)恒定流/非恒定流计算及模型调试要点;4)构筑物建模技术(桥梁、堰闸等);5)二维水动力模拟与溃坝分析;6)洪水风险图绘制。重点讲解水面线调试、糙率选取、报错分析等实操难点,通过案例教学实现从基础到工程应用的全流程技术掌握。该培训适用于防洪评价、河道治理等水利工程数值模拟需求。

2403_89974123的博客 1052

(算法)Tarjan离线算法解决LCA问题 (附POJ 1470 Closest Common Ancestors 代码)

对于最近公共祖先问题,我们先来看这样一个性质,当两个节点(u,v)的最近公共祖先是x时,那么我们可以确定的说,当进行后序遍历的时候,必然先访问完x的所有子树,然后才会返回到x所在的节点。这个性质就是我们使用Tarjan算法解决最近公共祖先问题的核心思想。      同时我们会想这

Bright 7911

告别虚拟机!在Windows上用WSL+VSCode搞定ArduPilot开发环境(保姆级避坑指南)

本文提供了一份详细的保姆级指南,介绍如何在Windows上使用WSL和VSCode搭建高效的ArduPilot开发环境,告别虚拟机性能损耗。从WSL安装、VSCode配置到ArduPilot固件编译和调试技巧,涵盖开发全流程中的关键步骤和常见问题解决方案,帮助无人机开发者快速构建专业级开发环境。

weixin_42543046的博客 461

InSAR_ISCE软件数据处理流程之:利用哨兵数据获取2015年尼泊尔地震同震形变场

ISCE软件数据处理流程之:利用哨兵数据获取2015年尼泊尔地震同震形变场 ISCE软件做干涉只需要将数据准备好,并配置好insarApp.xml文件即可进行。 一、数据准备 1、SAR数据获取 本利中使用的数据为欧空局提供的sentinel-1A/B数据,可从阿拉斯加数据服务器上下载,具体下载方法可参考: 哨兵数据下载方法. 2、哨兵卫星轨道文件下载 哨兵数据处理除了需要SAR影像外,还需提供轨道参数文件,可从https://qc.sentinel1.eo.esa.int/aux_poeorb/下载。下载

weixin_42128764的博客 1万+

poj 1470 Closest Common Ancestors 离线算法Tarjan求LCA

DescriptionWrite a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the

kongming_acm的专栏 679

Mac新手必看:用Homebrew安装wget的保姆级教程,附常见报错一键修复

本文为Mac新手提供使用Homebrew安装wget的详细教程,涵盖从Homebrew安装到wget基础使用的全流程,并附常见报错一键修复方案。通过本指南,用户可轻松掌握命令行下载工具wget的安装与使用技巧,解决Mac环境下资源下载的各种问题。

weixin_30635053的博客 367

LCA(最近公共祖先)——Tarjan

什么是最近公共祖先? 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点。 换句话说,就是两个点在这棵树上距离最近的公共祖先节点。 所以LCA主要是用来处理当两个点仅有唯一一条确定的最短路径时的路径。 常用来求LCA的算法有:Tarjan/DFS(离线),ST/倍增(在线)。 1,Tarjan tarjan...

weixin_34148456的博客 264

Wiley:International Journal for Numerical Methods in Biomedical Engineering投稿流程

中间过程省略,这个出版社很人性化,投稿的web UI也很好看。上传word之后,一步步跟着走就行,不用填太多东西。不要最后在最后检查阶段,重新上传word文件,这样会导致,之前针对word的操作全部改变。

Everyday try to become better -Kobe Bryant 2545

轴承寿命预测 | 基于BiLSTM双向长短期记忆神经网络的轴承寿命预测附matlab完整代码

轴承寿命预测 | 基于BiLSTM双向长短期记忆神经网络的轴承寿命预测附matlab完整代码

默默科研仔的博客 274

LCA的tarjan求法&&POJ 1470的辛酸历程

【LCA的线性解法】LCA(最近公共祖先)的问题十分常见。以前我单纯的认为,每次O(N)扫一遍每个节点的深度、再直接暴力求LCA的效率很高——Nlog(N)。但是往往树会退化成链(或者说它不平衡),如果询问次数多的话肯定TLE。离线解法TARJAN(这人好厉害,强连通算法也是他发明的)的效率则是O(N+Q),其中Q是询问个数。 【原理】用到了并查集的思想,每次对于一个点,处理询问中与他有关的一些

阿蒋的专栏 1539

RTDETR最强结构图 | 包含模型整体结构图 | 全模块展开图 | AiFi模块展开图

RT-DETR 最全结构图

Coding... 9600

poj 1470 Closest Common Ancestors

点击打开链接poj 1470 思路:LCA+Tarjan离线算法 分析: 1 处理输入的时候全部用scanf,不然会WA。注意掌握scanf的使用,scanf的灵活之处 2 因为有多次询问,所以要开个ans数组记录公共祖先的次数 3 有关字符串的处理: 1 空白字符会使scanf在读入的时候略去其中的一个或多个空白字符 。 2 getchar()使用来读入一个字符,所以可以单独处理一...

我是传奇 160

深度学习工具包 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")

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

【ICPC-27】poj 1470 Closest Common Ancestors

点击打开链接poj 1470 思路:LCA+Tarjan离线算法 分析: 1 处理输入的时候全部用scanf,不然会WA。注意掌握scanf的使用,scanf的灵活之处 2 因为有多次询问,所以要开个ans数组记录公共祖先的次数 3 有关字符串的处理: 1 空白字符会使scanf在读入的时候略去其中的一个或多个空白字符 。 2 getchar()使用来读入一个字符,所以可以单独处理一个字符...

藏经阁 | 玄苦 999

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

最形象的讲解,让你一次学会什么叫LCA离线算法tarjan

今天刚学了LCA离线tarjan算法。下面跟大家分享一下我的学习心得。 首先LCA是Lowest Common Ancestors的缩写。中文名为最近公共祖先。然后就是什么叫离线,与离线相对应的是在线算法(用dfs + rmp)的区别在哪里。 离线算法就是把所有的询问存起来,最后一次解决输出结果。结果有可能需要所有的询问(更新)之后才能得到。 在线算法就是每次询问一次,输出询问结果,每次的结果

creatorx的博客 4999

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

poj 1470 Closest Common Ancestors 【Tarjan 离线 LCA】

题目:poj 1470 Closest Common Ancestors 题意:给出一个树,一些询问。求LCA的个数、 分析:很简单的模板题目,但是模板不够优秀,一直wa...RE,各种错误一下午,终于发现自己模板的漏洞了。 AC代码: #include #include #include #include using namespace std; #def

大神养成中..... 1649

POJ 1470 LCA tarjan 离线算法

这里有个讲解非常明白,虽然没证明。点击打开链接  还有个数据结构的学习  点击打开链接 伸展树/Treap/划分树/ 归并树 步骤: tarjan算法的步骤是(当dfs到节点u时): 1 在并查集中建立仅有u的集合,设置该集合的祖先为u 1 对u的每个孩子v:    1.1 tarjan之    1.2 合并v到父节点u的集合,确保集合的祖先是u 2 设置u为已遍历 3

gg_gogoing的专栏 736

[POJ 1470]Closest Common Ancestors[离线LCA]

题目链接:[POJ 1470]Closest Common Ancestors[离线LCA] 题意分析: 给出多个查询,查询点u和v的最近公共祖先。输出每个点作为最近公共祖先在查询中出现的次数,0次的不输出。 解题思路: 离线LCA,需要用到tarjan。 和普通的tarjan差不多。多了两样东西: 1.u - > v回溯的时候,更新v的祖先为u 2.在递归结尾,检查查询。 个人感

闲停 693

POJ 1470 Closest Common Ancestors LCA离线Tarjin算法

http://poj.org/problem?id=1470 题意:给定一颗有根树,并且有Q次询问,格式是(u,v),求任意一次询问的两个结点的最近公共祖先。 算法:裸的LCA ,这是LCA的第一题,首先说下LCA离线Tarjin算法,其实质就是dfs +并查集。首先将所有的查询都先记录下面(因为是离线算法),然后就是从根结点对树进行一次dfs,复杂度为O(E) ,注意树用邻接矩阵存储。当

Chris_Home 642

Poj--1470(LCA,离线tarjan / 在线倍增)

2014-10-2612:13:10 思路:lca启蒙题,比较裸的一题,水过吧。有离线Tarjan和在线倍增两种做法。 离线Tarjan: 1 /************************************************************************* 2 > File Name: 1470.cpp ...

dingdi3021的博客 199

POJ - 1470 Closest Common Ancestors(LCA 离线tarjan)

Closest Common Ancestors Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tr...

YOONGI 288

【LCA基础】poj 1470 Closest Common Ancestors

LCA裸题

Gzbgreat的博客 468

LCA 离线tarjan算法

对于最近公共祖先问题,我们先来看这样一个性质,当两个节点(u,v)的最近公共祖先是x时,那么我们可以确定的说,当进行后序遍历的时候,必然先访问完x的所有子树,然后才会返回到x所在的节点。这个性质就是我们使用Tarjan算法解决最近公共祖先问题的核心思想。       同时我们会想这个怎么能够保证是最近的公共祖先呢?我们这样看,因为我们是逐渐向上回溯的,所以我们每次访问完某个节点x的一棵子树,我们

Everything can be done! 315

POJ 1470 Closest Common Ancestors (LCA 离线算法)

Closest Common Ancestors Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancest...

zhe 264

POJ 1470 Cloest Common Ancestor(用Tarjan查询LCA)

题目链接; POJ 1470 Cloest Common Ancestor 题意: 给一个nn个节点,n−1n-1条边的树,有多组查询两个节点的最近公共最先,按编号从小到大输出至少作为一次查询的最近公共祖先的节点编号和相应的次数。 数据范围:n≤900n\leq 900 分析: TarjanTarjan离线处理。当然使用基于RMQRMQ的在线处理方式也应该可以 。 算法

ramay7 548
上一篇: 《算法艺术与信息学竞赛》题目-提交方式对照表
下一篇: POJ 1330 Nearest Common Ancestors (LCA&Tarjan算法)
synapse7
博客等级 码龄14年 432粉丝 701原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值