POJ1741——Tree 基于点的分治

双向全桥隔离DC-DC变换器(DAB)的调制优化与挑战解析 本文深入解析了双向全桥隔离DC-DC变换器(DAB)的核心调制技术及其面临的挑战。文章从基础的相移调制(SPS)入手,逐步探讨了扩展相移(EPS)、双重相移(DPS)等进阶策略,并重点分析了电流优化与零电压开通(ZVS)等硬核优化技巧。针对DAB在实际应用中存在的建模困难、多模块并联及数字实现等挑战,提供了实用的应对策略与设计思路,旨在帮助工程师在性能、复杂度与成本之间找到最佳平衡。 阅读详情
Tree
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11229 Accepted: 3515

Description

Give a tree with n vertices,each edge has a length(positive integer less than 1001).
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.

Input

The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l.
The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0

Sample Output

8

Source

LouTiancheng@POJ


这题要我们计算所有满足距离之和小于等于k的点对的数目, n最大有10000,显然O(n^2)的做法是行不通的,看了部分09年漆子超 《分治算法在树的路径问题中的应用》,于是试着去理解这种做法,然后写下了这篇博客。

显然我们可以知道,这样的点对可以分为2种
1) 经过树的根
2)在某一棵子树里面

而2显然可以递归处理,所以我们把目光放在1

我们设dist[i]表示点i到根的距离,那么我们1要求的就是 dist[i] + dist[j] <= k && i,j 属于不同的子树,从论文中我们可以学到,      把所要求的转化为dist[i] + dist[j] <= k的所有点对减去dist[i] + dist[j] <= k && i,j处在同一个子树里的点对,这就是我们要求的东西

而统计点对数,我们可以在O(n)的时间内完成,操作就是给数组排个序,然后左边,右边分别移动下标,边移动边统计就OK了,复杂度O(n*logn)

接下来还有一个问题,如果这个树是一条链,那么每次去递归子树,复杂度显然会到达O(n^2),所以这里又要引出一个内容:树的重心

树的重心是这样定义的:删掉重心以后,树被分为几个部分,使得那几个部分里点最多的那个部分的点数最少

树的重心我们可以通过树形dp在O(n)时间里求得,可以证明,如果每次都选取子树的重心作为根,那么递归次数最多不超过logn次,所以整个过程下来,复杂度是O(n*logn*logn)

先放上我的代码吧,也是结合了网上各个前辈的做法的
PS:我还想在代码下面写点关于代码理解的

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010;
const int inf = 0x3f3f3f3f;
int num[N];
int head[N];
int dist[N];
int dp[N];
bool vis[N];
struct node
{
	int weight;
	int next;
	int to;
}edge[N << 1];

int tot, root, n, k, size, res, ans;

void addedge(int from, int to, int weight)
{
	edge[tot].weight = weight;
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

void get_root(int u, int fa)
{
	num[u] = 1;
	dp[u] = 0;
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (v == fa || vis[v]) //-------------------------A
		{
			 continue;
		}
		get_root(v, u);
 		num[u] += num[v];
		dp[u] = max(dp[u], num[v]);
	}
	dp[u] = max(dp[u], size - num[u]);
	if (dp[u] < dp[root])
	{
		 root = u;
	}
}

void calc_dist(int u, int fa, int d)
{
	dist[res++] = d;
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (v == fa || vis[v])//--------------------------B
		{
			continue;
		}
		calc_dist(v, u, d + edge[i].weight);
	}
}

int counts(int u, int d)
{
	res = 0;
	calc_dist(u, -1, d);
	int ans = 0;
	sort(dist, dist + res);
	int i = 0;
	int j = res - 1;
	while (i < j)
	{
		while (i < j && dist[i] + dist[j] > k)
		{
		       j--;
		}
		ans += j - i;
		i++;
	}
	return ans;
}

void solve(int u)
{
	ans += counts(root, 0);
	vis[root] = 1;
	for (int i = head[root]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (vis[v])
		{
			continue;
		}
		ans -= counts(v, edge[i].weight); // ------------------------------C
		root = 0;
		dp[0] = size = num[v];
 		get_root(v, -1);
		solve(v);
 	}
}

int main()
{
	 int u, v, w;
	 while (~scanf("%d%d", &n, &k))
	 {
		if (!n && !k)
		{
	         	break;
                }
		memset ( head, -1, sizeof(head) );
		memset ( num, 0, sizeof(num) );
		memset ( vis, 0, sizeof(vis) );
		tot = 0;
	        ans = 0;
		for (int i = 0; i < n - 1; ++i)
		{ 
 			scanf("%d%d%d", &u, &v, &w);
 			addedge(u, v, w);
			addedge(v, u, w);
		}
 		root = 0;
		dp[root] = size = n;
		get_root(1, -1);
 		solve(1);
 		printf("%d\n", ans);
 	}
 	return 0;
}

先来解释下注释C吧,
ans -= counts(v, edge[i].weight);
为什么是edge[i].weight而不是0,今天看的时候不知道是不是脑子短路了一直没理解,其实是这样的,root(当前树根)这棵树统计完了点对以后,其实已经包含了某些点对,它们处在同一个子树里,但是距离和确实小于等于k的,那么这里小于等于k是基于它们到点root的,所以在处理v这个子树的时候,我们需要把这样的点对给去掉,那么基准点当然还是以root为主,这样才能准确地去掉这些点对,所以要加上root --- v这条边上的权值

再来看A和B,为什么要加一个fa,由于建立的是无向边,所以得加一个fa,放止从子树跑到根上去
vis数组是为了防止访问到已经处理过的重心(子树的根),我们每次找到一个重心,要拿它当根来处理的时候,都会把它标记为已经访问过,如果不慎访问到了已经访问过的root(即子树),那么分治就被干扰了,或者说已经被破坏了,所以这个vis数组是一定要加上去的
基本就讲到这里,如果发现本人所写有错误,欢迎给我留言



vue支付项目-APP支付宝支付功能 如果用户已安装支付宝客户端,商家 App 唤起支付宝完成支付,支付完后跳回到商家 App 内,最后展示支付结果。如果用户没有安装支付宝客户端,商家 App 内会调起支付宝网页支付收银台,用户登录支付宝账号,支付完后展示支付结果。1.点击按钮开通或者续费,会验证是否选择会员商品以及对协议是否查看选中(这里根据项目需要自行验证,可忽略),验证通过打开选择支付方式弹窗页面,选择微信支付或者支付宝支付;4.可取消支付,放弃支付会返回会员页面,页面提示支付取消;1.判断支付方式,如果是2,则是支付宝支付方式。 阅读详情

相关推荐

斑马打印机状态监控进阶:不用SDK也能获取打印状态的3种野路子

本文介绍了三种不依赖官方SDK的斑马打印机状态监控方法,包括SGD命令、TCP端口嗅探和共享打印机日志分析。这些方案适用于老旧系统或网络受限环境,帮助运维人员实时掌握打印状态,提升设备管理效率。特别适合工业自动化和物流仓储场景中的应急处理。

weixin_42628688的博客 427

poj 1741 Tree (点的分治)

题意:给你一棵树,问在树上有多少点对之间的最短距离小于等于K,且(a,b)和(b,a)算一个 思路:这题可以用点的分治来做,选取点为这棵树的重心,即最大子树最小的点。然后可以分两种情况进行处理,过这一点与不过这一点,而不过这一点就递归处理它的子树即可。 在统计的时候要注意,先算出到根节点的距离,然后排序,存入一个队列当中,统计时则可以根据头尾两个数相加来决定中间数的大小,因而 可以进行快速的统

huolongshenghu的专栏 628

旋转矩阵推导

推导前提: 坐标使用右手坐标系,角度逆时针旋转为正。 绕X轴旋转角度为 俯仰角 即Pitch 绕Y轴旋转角度为 偏航角 即Yaw(Head) 绕Z轴旋转角度为 翻滚角 即Roll 一、平面二维坐标点的旋转 如右图所示,根据三角函数关系,可以列出向量OP与OP’的坐标表示形式: x = |OP|•cosα x′ = |OP|•cos(α+β) y = |OP|•sinα y′ = |OP|•sin(α+β) 将P′(x′,y′)表达式展开: x′ =

suixinger_lmh的随笔 1384

POJ - 1741 Tree (点分治 求路径距离小于等于k的点对)

Tree Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices...

YOONGI 1192

示波器实测RGMII信号:从125MHz时钟判断千兆模式,到时序抓取与PHY寄存器避坑

本文详细介绍了使用示波器实测RGMII信号的完整流程,包括125MHz时钟频率判断、时序参数测量及PHY寄存器配置避坑指南。通过实战操作和典型故障案例分析,帮助硬件工程师快速定位和解决千兆以太网接口的调试问题,提升通信稳定性。

weixin_30015371的博客 260

DPABI实战:手把手教你处理小鼠静息态fMRI数据(从Voxel Size到SPM完整流程)

本文详细介绍了使用DPABI处理小鼠静息态fMRI数据的完整流程,从Voxel Size调整到SPM分析,涵盖预处理、质量控制和可视化等关键步骤。针对小鼠数据特有的低分辨率和信噪比问题,提供了实战建议和避坑指南,帮助研究人员高效完成数据分析。

weixin_42663213的博客 477

POJ 1741.Tree 树分治 树形dp 树上点对

Tree Time Limit:1000MS Memory Limit:30000K Total Submissions:24258 Accepted:8062 Description Give a tree with n vertices,each edge has a length(positive integer les...

weixin_30273501的博客 107

envi5.3处理高分二号影像数据详细过程记录

全文根据此篇文章进行数据处理: tm影像辐射定标_高分二号影像数据预处理及裁剪过程 其他参考: 学习笔记---遥感影像辐射定标与大气校正 利用ENVI自带全球DEM数据计算区域平均高程 高分二号数据处理流程(有高分二号的数据信息) GF2有多光谱和全色两个数据,MSS是多光谱的,PAN是全色的,多光谱的分辨率低,全色的高,需要把这两个数据融合,融合之前需要做定标等处理,获取经纬度。 一、多光谱影像处理 1. 辐射定标 以这个为例: 其他路径\2022.4.2...

ligous的博客 5万+

POJ 1741 Tree(点分治点对<=k)

Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of ver...

weixin_30737363的博客 88

Autoware1.14-摄像头目标检测YOLO-V3

在Autoware1.14中部署YOLOV3

weixin_43687753的博客 3139

养龙虾之_因特尔UHD/ULtra/Arc核显跑大模型_安装ollama_跑qwen3.5:0.8b/9b_使用copaw1.0.1测试_并配置copaw技能效果炸裂---AI大模型应用探索0019

摘要:本文探讨了利用Intel UHD770核显运行大语言模型的可能性。通过使用Intel发布的ipex-llm项目工具包,成功在核显上运行了Qwen3.5系列模型。测试发现0.8B小模型运行流畅(3-5秒响应),但智能程度有限;而9B大模型虽然效果更好,但响应时间长达2分钟,且GPU/CPU占用率不高。实验证明Intel核显确实可以运行LLM,但实际效果和性能表现仍有提升空间,特别是对大模型的支持尚不理想。

添柴程序猿的专栏 236

POJ 1741 Tree (男人八题 - 树分治[点分])

题意: 树上n 个点, 求有多少点对满足距离不超过K 思路: 树上点分治 入门题目。 我们每次递归 都要从这棵树的重心开始递归。  从这个点这出其他点到这个点距离来, 然后我们看看有几个点对 距离和满足小于等于K, 这肯定是一部分贡献, 但是有个问题, 这样计算,只有 一个点在一个子树,另一个点在另一个子树才能满足这个距离公式, 如果同在一棵子树, 那么显示式子不成立, 所以,我们需要把这

aozil_yang的博客 522

机器学习的低代码与无代码平台:便捷入门之路

机器学习领域正涌现出大量低代码和无代码平台,使非技术人员也能轻松应用AI技术。低代码平台(如PyCaret、H2O AutoML)需少量编程但灵活性强,而无代码工具(如Google Cloud AutoML、Teachable Machine)则完全无需编程基础。这些平台覆盖图像识别、文本处理、业务预测等多种场景,适合快速部署简单项目。但专业人士仍需注意,这些工具无法替代复杂场景下的定制化开发。随着技术进步,低门槛AI工具正在改变各行业的创新方式。

2501_90323865的博客 154

解题报告:POJ1741 Tree 树上点分治(经典好题)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19522   Accepted: 6388 Description Give a tree with n vertices,each edge has a length(positive integer les

Hee的博客~ 514

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

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

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

POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数

Tree Time Limit:1000MS Memory Limit:30000K Total Submissions:12276 Accepted:3886 Description Give a tree with n vertices,each edge has a length(positive integer les...

weixin_30436891的博客 362

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

poj1741_Tree(树的点分治入门题)

题目链接:poj1741_Tree 题意: 给你一颗n个节点的树,每条边有一个值,问有多少点对(u,v),满足u->v的最短路径小于k。 题解: 典型的树的分治,板子题。 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #define F(i,a,...

weixin_30646315的博客 103

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

【树上点分治1】Tree POJ 1741(容斥原理)

【树上点分治1】Tree POJ 1741(容斥原理) 学习博客 题意: 给定一棵树,要求找出树上两点之间距离不超过k的点对有几个。 思路: 以A为根节点,把树上路径分成两部分 1.经过A节点 用tmp[]存储从A节点到各节点的距离,那么ans=tmp[i]+tm[j]<=k,但是若i,j在同一颗子树,比如A->B->D和A->B->E,则不能进行合并,所有需要减去在同一颗子树的符合情况的点对,这刚好可以对B子树进行相同的处理,以B为根节点算一次ans’,再用A节点的ans.

weixin_45722843的博客 194

【暖*墟】 #数据结构进阶# 点分治

点分治,是一种处理树上路径问题的工具,适用于树上的求和问题。 给定一棵树和一个整数k,求 树上边数等于k的路径 有多少条。 做法1:枚举不同的两个点,用dfs算出距离、并统计求值。O(n^3)。 做法2:找根,求出每个点到根的距离,枚举两点求lca。O(n^2logn)。 做法3:考虑形成路径的情况,假设一条满足条件的路径经过点x, 那么这条路径 ①在x的一个子树里(以...

Christy2222的博客 245

[POJ1741]Tree(点分治+讲解)

点分治第一弹!

薇小薇 452

poj1741(点分治)

点分治和树dp的关联还是有点大。。所以这锅还是窝的。。点分治主要用于处理点对问题。。点分治比较套路。。做几道题应该就没什么问题了?基本思想就是选取一个点(为重心时复杂度最佳)为根,然后把问题分解成处理经过根的链和去掉根后的子树中的子问题。。然后最关键的就是怎么求经过根的链了。。。这里用的是dfs处理出深度然后将深度和小于k的记录下来,然后排序之后就可以直接用双指针计数了。。复杂度是O(n)然而会发...

qkoqhh 268

点分治—学习笔记【例题POJ - 1741 Tree && 洛谷P3806点分治1】

我们直接引入这样问题 给你一棵TREE(n&amp;amp;amp;amp;amp;amp;amp;amp;lt;=40000),以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K (来自Tree的问题描述) 看到数据范围就知道这肯定是不能暴力的了 n^2的枚举加路径长度计算,都不知道被人家甩了几条街 所以我们便有了优雅的暴力—点分治 我们先随意指定一个根rt,将这棵树转化成无根树 不难发现树上的路径分为两类 经过根节点rt的...

niiick 492

poj 1741 洛谷 3806 【模板】点分治1 树的分治 点分治

题目背景感谢hzwer的点分治互测。题目描述给定一棵有n个点的树询问树上距离为k的点对是否存在。输入输出格式输入格式:n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径接下来m行每行询问一个K输出格式:对于每个K每行输出一个答案,存在输出“AYE”,否则输出”NAY”(不包含引号)输入输出样例输入样例#1:2 1 1 2 2 2输出样例#1:AYEhttps://www.luogu....

Panda!!!!!! 433

poj 1741 点分治论文题

题意: 给你一棵树, 让你求这棵树上满足dis(u, v) <= k的点对有多少个。 分析: 首先, 对于直接想到的办法。lca预处理然后暴力,复杂度n^2,显然复杂度太大。 那么我们就有了树上分治的思想; 首先, 对于这个问题, 我们可以看出只有如下三种情况: 然后分治处理。 这里要注意,分治的时候要求重心, 因为重心可以保证

' 泪 添 无 言's blog 525

POJ 1741 Tree (树分治模板题)

点击打开链接 题意:给出一棵树,求出两点之间距离小于等于k的点对数目。 解法:树的考查十分常见,原因就在于树有一般图不具备的约束条件。 如果这是一个一般图,我们想的是,做一次LCA,然后暴力两个点,看是否符合,复杂度达到O(n²),对于题目有1e4个点而且还是多组输入显然是无法接受的。 这时候我们就要想到树分治的思想。 求两个点的距离是否小于等于k,可以分为两种情况。 我们假定一个

llzhh的博客 398

POJ 1741 Tree, 树的重心, 树分治, 点分治

最近在学习树的分治,算是比较难,而且代码量比较大的一块。随便拿一道题来就有上百行,故写一篇文章来总结一下这方面的框架。 POJ这一题应该算是树分治的入门题,顺便用这一题来详细说明树分治的一些具体内容。 http://poj.org/problem?id=1741 Tree Time Limit: 1000MS Memory Limit: 30000K Desc

Stitch_11752的博客 1325

poj1741 Tree

Poj1741 Tree 解题报告 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15559   Accepted: 5055 Description Give a tree with n vertices,each edge has

ACoder 509
上一篇: POJ3311——Hie with the Pie
下一篇: POJ1987——Distance Statistics
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值