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

5060TI显卡深度学习环境搭建:CUDA12.8与CUDNN9.8.0的完美搭配 本文详细介绍了在Win10系统下为5060TI显卡搭建深度学习环境的最佳实践,重点配置CUDA12.8与CUDNN9.8.0的完美组合。通过优化OpenCV编译选项和YOLOv5环境设置,显著提升模型训练和推理效率,帮助开发者充分发挥5060TI显卡的性能潜力。 阅读详情
树的最近公共祖先(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;
}

【多模态实验总结】整理了几十篇文章,理清 多模态目标检测 在写论文中需要做哪些实验 此篇文章中将多模态目标检测所做的实验,分成了各个类别,目的是为了对多模态的实验有一个清晰的理解。可以看到,很多表格中放的并不是做了单一的某一个对比,而是将提出的模型,在不同的骨干、数据集上的效果与基线进行对比,使内容更全面。因此这里提到的各种实验,大家在后续改进写论文过程中可以根据自己的论文内容进行整合。对于这里提到的这些文章,大家都可以看看,我只是按照我这里的分类截取了部分实验,这里提到的所有实验在每篇文章中也基本能看到相应的实验。 阅读详情

相关推荐

缠论 公式 自动画线 画笔 中枢 买卖点 源代码

【代码】缠论 公式 自动画线 画笔 中枢 买卖点 源代码。

weixin_40835859的博客 2万+

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

记录Nvidia AGX Orin的配置日常(二)设置上电自动开机和远程桌面

因为要拿AGX作为机器人的主控板,所以需要给他设置一下通电后无需按电源键就自动开机,然后再配置一下远程桌面。

AutomaticForever的博客 1363

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

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

Bright 7911

MySQL常见面试题汇总(建议收藏!!!)

目录一、索引相关(1)什么是索引?(2)索引是个什么样的数据结构呢?(3)为什么使用索引?(4)Innodb为什么要用自增id作为主键?(5)Hash 索引和 B+ 树索引有什么区别或者说优劣呢?(6)什么是聚簇索引(6)非聚簇索引一定会回表查询吗?(6)聚簇索引和非聚簇索引的区别(6)MyISAM和InnoDB实现B+树索引方式的区别是什么?(6)MySQL中有四种索引类型,可以简单说说吗?(6)覆盖索引是什么?(6)联合索引是什么?为什么需要注意联合索引中的顺序?(6)创建的索引有没有被使用到?或者说怎

蔡政洁的博客 7万+

ios生成海报使用dom-to-image中toPng报错isTrusted:true

主要是ios中safari浏览器解析错误,引起的报错,主要引起原因toPng会调用toSvg,toSvg直接将html标签内容通过svg中foreignObject标签直接包裹,然后转base64, 通过data:image/svg+xml;解决办法:二次生成(调用两次),判断ios or 安卓,生成第一次生成,不用参数,例如:scale: 3,在第二次生成再进行清晰度操作,避免多次生成,引起性能消耗。以上种种很多案例,使用canvase进行转换的,标签丢失,直接toPng版本的,兼容性问题。

weixin_44601570的博客 2105

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

如何实现chrome谷歌浏览器多开(独立环境 独立cookie)

由于各种各样的原因,你可能需要在一个电脑登录某个平台,比如一个电脑登录3个公众号,或者3个知乎等等。最简单的方案是,直接安装3个不同的浏览器,比如一个谷歌浏览器,一个火狐浏览器,一个 Edge 浏览器。这个方案也能解决,但如果想多开10个呢?安装10个不同的浏览器也能解决,但感觉这样比较复杂,另外一方面安装太多的应用,会导致电脑卡顿。这里给大家介绍,我在 Win 和 Mac 系统下的躲开解决方案。Win 系统下的解决方案最为简单,原理就是给不同的谷歌浏览器,指向不同的用户存储路径即可。

lzqial1987的专栏 1万+

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

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

weixin_34148456的博客 264

以太网物理层协议整理(2)-万兆以太网

在万兆以太网标准化过程中,IEEE和10GEA(万兆以太网联盟)是两个最重要的组织。 万兆以太网标准和规范都比较繁多,在标准方面,首先有2002年的IEEE 802.3ae,后来也不断有新的万兆以太网规范推出,如2006年的IEEE 802.3an标准(10GBase-T)就推出了基于双绞铜线的万兆以太网规范,同样的IEEE 802.3aq标准推出了基于光纤的10GBase-LRM;2007年的

NowOrNever 3万+

R语言Dunnett’s检验实战:寻找差异组之间的效果

总结起来,本文以R语言为工具,演示了如何使用Dunnett’s检验来找出单因素方差分析中不同组之间的效果差异。在本篇文章中,我们将通过R语言实战演示如何使用Dunnett’s检验来寻找不同组之间的效果差异。在分析结果时,通常我们会关注具有显著差异的组别,并基于Dunnett’s检验结果进行进一步的解释和讨论。首先,我们需要准备一组数据,假设我们有一个实验,涉及到四个不同的处理组和一个对照组。当我们发现方差分析结果显示存在显著差异时,我们可以进一步进行Dunnett’s检验来确定具体哪些组之间存在差异。

CoderExtra的博客 1104

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

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

阿蒋的专栏 1539

uniapp - H5人脸识别认证与活体检测功能(纯前端免费方案+微信sdk人脸识别)完整源码,微信公众号网页/h5端人脸识别功能人脸核身(微信方案提供后端spring boot+thinkphp源码

uniapp实现人脸认证,uniAPP h5手机拍照+人脸识别,免费人脸识别插件,前端人脸识别解决方案,uniapp H5人脸识别带前后摄像头,人脸识别在H5页面进行采集,微信公众号H5人脸核身,H5端人脸识别功能实现,免费h5人脸识别插件方案,微信公众号H5网页实现人脸识别,uniapp人脸监测,uniapp人脸生物监测,使用uniapp开发H5人脸采集功能/人脸识别功能,uniapp项目—h5端人脸采集功能实现,uniapp vue3人脸核身,H5活体检测认证,人脸认证张嘴摇摇头指引动作,人脸核身 微信

🏆 前端领域优质创作者 3809

poj 1470 Closest Common Ancestors

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

我是传奇 160

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【ICPC-27】poj 1470 Closest Common Ancestors

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

藏经阁 | 玄苦 999

深度学习工具包 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离线算法tarjan

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

creatorx的博客 4999

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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、付费专栏及课程。

余额充值