Jzoj3555 树的直径

TiDB实战篇-数据库写入慢常见处理方式 阅读详情
科学家在观测一棵大树,这棵树在不断地生长,科学家给这棵树的每个节点编了号。开始的时候,这棵树很小只有4个节点,一号点为根,其他三个节点挂在上面。

在接下来的M次观察中,科学家每次都能看见这棵树从叶子处长出新的两个节点来。如果当前这棵树有N个节点,那么这棵树的新的两个节点的编号分别为N+1,N+2。科学家记录下了这棵树生长的过程,需要你帮着计算这棵树实时的直径。树的直径就是这棵树最远的两个节点的距离。

每行一个整数x,代表这棵树的编号为x的节点下面又长了两个叶子节点。保证每次生长的节点都是叶子节点

好久没在jzoj上切题了,估计ranking都要掉了赶紧做一做今天的模拟题

还好第一题还比较简单,直接将新建节点和原来直径合并就好了,求lca就用倍增ok

#pragma GCC opitmize("O3")
#pragma G++ opitmize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int f[200010][18],d[200010],n;
inline void link(int p,int x){
	f[x][0]=p; d[x]=d[p]+1;
	for(int i=1;i<18;++i) f[x][i]=f[f[x][i-1]][i-1];
}
inline void swim(int& x,int d){
	for(int i=0;d;++i,d>>=1) if(d&1) x=f[x][i];
}
inline int gLca(int x,int y){
	if(d[x]>d[y]) swap(x,y);
	swim(y,d[y]-d[x]);
	if(x==y) return x;
	for(int j=17;;){
		for(;~j&&f[x][j]==f[y][j];--j);
		if(j<0) return f[x][0];
		x=f[x][j]; y=f[y][j];
	}
}
int main(){
	d[2]=d[3]=d[4]=1; *f[1]=0;
	f[2][0]=f[3][0]=f[4][0]=1;
	scanf("%d",&n); int x=2,y=3,r=2;
	for(int	p,c,t=4;n--;){
		scanf("%d",&c);
		link(c,++t); link(c,++t);
		p=gLca(x,t);
		if(d[x]+d[t]-2*d[p]>r){ r=d[x]+d[t]-2*d[p]; y=t; }
		p=gLca(y,t);
		if(d[y]+d[t]-2*d[p]>r){ r=d[y]+d[t]-2*d[p]; x=t; }
		printf("%d\n",r);
	}
}

国网DLT698.45协议——采集系统、数据交换(三) 物理层规定了通信单元或其他终端设备与多功能电表的通信接口,接口的物理与电气特性。常见的物理层传输方式有对于RS485、红外、载波通信等通信信道。将数据包装成可传输的帧格式。1、根据通信配置参数完成端口的初始化配置,通信状态初始化为准备接收状态,初始化相关计数器及缓冲区指针。2、链路层及控制子模块负责通信数据的收发控制,各类超时问题的处理。无通信数据接收时,定时初始化通信端口。3、完成协议帧的解析及应答。 阅读详情

相关推荐

避坑指南:英飞凌TC3XX的MCMCAN初始化常见错误及解决方法

本文针对英飞凌TC3XX系列MCU的MCMCAN模块初始化难题,深入剖析了寄存器配置时序、总线监控模式误用、中断映射错误及Message RAM地址冲突四大常见陷阱。通过实战代码示例和诊断流程,提供了清晰的避坑指南和解决方案,帮助嵌入式开发者高效解决CAN FD通信故障,确保汽车电子与工业控制项目的稳定运行。

weixin_29193259的博客 250

jzoj3555 树的直径

Description科学家在观测一棵大树,这棵树在不断地生长,科学家给这棵树的每个节点编了号。开始的时候,这棵树很小只有4个节点,一号点为根,其他三个节点挂在上面。 在接下来的M次观察中,科学家每次都能看见这棵树从叶子处长出新的两个节点来。如果当前这棵树有N个节点,那么这棵树的新的两个节点的编号分别为N+1,N+2。科学家记录下了这棵树生长的过程,需要你帮着计算这棵树实时的直径。树的直径就是这棵

ChickenT的博客 309

汽车级智能栅极驱动器深度解析 | 连载01:DRV871x-Q1系列产品架构与核心特性

集成度:单芯片集成多通道驱动器、电源管理、电流检测智能化:自适应控制算法,减少外部元件可靠性:多重保护机制,满足汽车级要求灵活性:SPI和硬件两种接口,适应不同应用需求。

VehSwHwDeveloper的博客 1168

【jzoj5042】【最小直径】【树形动态规划】【贪心/结论】

题目大意你有一个n个点m条边的森林,编号从0开始,边有边权,你现在要添加若干边权为L的边,满足:1、最后n个点构成一颗树。2、这棵树的直径尽量小。请你求出这个最小的直径是多少。解题思路求出每一个连通块的半径,答案及第一大加第二大+L和第二大加第三大+2L取最大。code#include<set> #include<cmath> #include<cstdio> #include<cstring> #

inklutcuah’s blog 792

FLUKA软件在Ubuntu的安装指南(亲测有效)

FLUKA其强大的蒙特卡洛模拟能力使得用户能够模拟复杂的辐射相互作用过程

chencheng1127的博客 2896

TI LP87745-Q1汽车雷达PMIC评估:从硬件设计到性能测试全解析

在汽车电子和高级驾驶辅助系统(ADAS)中,电源管理单元(PMIC)是确保传感器稳定可靠运行的核心。其工作原理在于集成多路高效电源转换器(如Buck、Boost)和精密监控电路,通过数字接口实现灵活配置与实时诊断。这项技术的核心价值在于为毫米波雷达等关键负载提供低噪声、高精度、满足严格时序与功能安全(ASIL)要求的供电方案,从而提升整个感知系统的信噪比与可靠性。其典型应用场景包括自动驾驶域控制器、车载传感器融合模块等。本文以德州仪器(TI)的LP87745-Q1专用PMIC及其评估模块(EVM)为例,深入

weixin_27017211的博客 315

5042. 【NOI2017模拟4.4】最小直径

Description 你有一个n个点m条边的森林,编号从0开始,边有边权,你现在要添加若干边权为L的边,满足: 1、最后n个点构成一颗树。 2、这棵树的直径尽量小。 请你求出这个最小的直径是多少。 Input 第一行三个整数n,m,L。 接下来m行,每行三个整数u,v,w,表示u和v之间有长为w的边。 Output 一行一个整数,表示最小直径的长度。 Sample Input 1...

zsjzliziyang的博客 294

使用TMS320F28335控制四位共阳数码管

简要阐述利用F28335控制四位共阳数码管的整个过程,包括原理、思路、部分代码以及调试过程。

liujunhaozuishuai的博客 2663

[ZOJ3555][GDKOI2014模拟]树的直径

找不到原题了…. Description 科学家在观测一棵大树,这棵树在不断地生长,科学家给这棵树的每个节点 编了号。开始的时候,这棵树很小只有4 个节点,一号点为根,其他三个节点挂在上面。 在接下来的M 次观察中,科学家每次都能看见这棵树从叶子处长出新的两 个节点来。如果当前这棵树有N 个节点,那么这棵树的新的两个节点的编号分 别为N+1,N+2。科学家记录下了这棵树生长的过程,需要...

Beginner_df016 286

基于若依的ruoyi-nbcio-plus支持VForm3表单字段数据保存到数据库的一种方法——全网首创(一)

基于若依的ruoyi-nbcio-plus支持VForm3表单字段数据保存到数据库的一种方法——全网首创(一)

宁波阿成的博客 666

注意力机制篇 | YOLOv8改进之在C2f模块添加EMA注意力机制(附2种改进方法)

EMA(Exponential Moving Average)注意力机制是一种用于增强模型性能的注意力机制,它通过对模型的特征图进行加权平均来提取更有用的特征信息。具体来说,EMA注意力机制通过引入一个权重因子来调整特征图中每个位置的重要性,从而使模型能够更好地关注重要的特征。🌈

小哥谈 4185

jzoj3555 【GDKOI2014模拟】树的直径 lca+离线

Description科学家在观测一棵大树,这棵树在不断地生长,科学家给这棵树的每个节点编了号。开始的时候,这棵树很小只有4个节点,一号点为根,其他三个节点挂在上面。在接下来的M次观察中,科学家每次都能看见这棵树从叶子处长出新的两个节点来。如果当前这棵树有N个节点,那么这棵树的新的两个节点的编号分别为N+1,N+2。科学家记录下了这棵树生长的过程,需要你帮着计算这棵树实时的直径。树的直径就是这棵树最

olahiuj的博客 513

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

【题解】树上路径

题目描述 一棵树的构造过程为:首先以1号点为根,然后依次加入2~n号点。加入i号点时,在1~i-1点中选择一个点为f[i],将i号点与其相连接。Yuri想要求出,每次加点之后路上的最长路径长度。 输入格式 第一行一个整数n,表示树的节点个数。 第二行n-1个整数,第i个整数表示f[i+1]。 输出格式 一行n-1个整数,分别表示加完2~n号点后,树上最长路径的长度。 样...

C@bMv7GRVt)Q 264

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

【JZOJ 5776】【NOIP2008模拟】小x游世界树 (树)

问题描述 小x得到了一个(不可靠的)小道消息,传说中的神岛阿瓦隆在格陵兰海的某处,据说那里埋藏着亚瑟王的宝藏,这引起了小x的好奇,但当他想前往阿瓦隆时发现那里只有圣诞节时才能到达,然而现在已经春天了,不甘心的他将自己的目的地改成了世界树,他耗费了大量的时间,终于将自己传送到了世界树下。世界树是一棵非常巨大的树,它有着许许多多的枝条以及节点,每个节点上都有一个平台。好不容易来到传说中的世界树下,...

Fallen_Angel001的博客 471

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

寒假2019培训:树的直径,重心&&基环树????

今天学习了树的直径,重心和基环树,直切主题吧~~ 一,树的直径 ---------------------------------------------------第一次更新于2019.2.13----------------------------------------------- After hearing about the epidemic of obesity in the...

星暗宇的博客 514

ZOJ 3684 Destroy 树的直径+二分

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意:题中给出一个树,结点是城市,路径有长度和炸毁需要的武器单位数,,X单位的武器能同时炸毁所有X单位以下的路径。问需要多少单位的武器能使所有边疆城市和中心城市不联通。 思路:中心城市就是整棵树的根(到所有城市中的最长距离最短的城市),树的根一定在树的直径上

看曙光 1210

#spfa,离散#poj 3171 jzoj 1254 洛谷 4644 清理牛棚 Cleaning Shifts

没有边权的题解poj 2376 题目 分析 代码 #include &amp;amp;lt;cstdio&amp;amp;gt; #include &amp;amp;lt;algorithm&amp;amp;gt; #include &amp;amp;lt;vector&amp;amp;gt; #include &amp;amp;lt;queue&amp;amp;gt; s

lemondinosaur的博客 299

bzoj 3246: [Ioi2013]Dreaming 树形dp+树的直径

题意你有一个n个点m条边的森林,编号从0开始,边有边权,你现在要添加若干边权为L的边,满足: 1、最后n个点构成一颗树。 2、这棵树的直径尽量小。 请你求出这个最小的直径是多少。 n<=500000分析设d[i]表示i到其所在子树的最远的点的距离。显然每一棵树与其他树相连的必然都是同一个点,且必然是d[i]最小的那个点。然后把每棵树缩成一个点,点权为其最小的d[i]。那么最后一定是构成一个菊

beginend 515

【贪心】JZOJ_3929 创世纪

贪心

GOODPLACE 476

【JZOJ A组】树

Description 我们有一颗从1到n编号的n个结点的树,此外,您将从树中获得M个节点对,形式为(a1,b1),(a2,b2),…(am,bm). 我们需要给每一条边定向,使得每一对节点对存在一条从ai到bi或从bi到ai的路径。 现在要求方案数,对10^9+7取mod即可。 Input 第一行两个整数,n,m 接下来n-1行,每一行两个整数,描述一条树边。 接下来m行,描述...

Brute♂force 784

【BZOJ5314】【JSOI2018】—潜入行动(树形dp)

传送门 似乎和曾经一道简单树形dpdpdp类似? f[i][j][0/1]f[i][j][0/1]f[i][j][0/1]表示iii的子树点亮jjj个,当前这个点点不点亮的方案 只不过这个是自己不会点亮自己 于是f[i][j][0/1][0/1]f[i][j][0/1][0/1]f[i][j][0/1][0/1]多一维表示自己是否被点亮 要考虑的情况似乎有点多? 卡空间毒瘤差评 #include&...

Stargazer的博客 416

【NOIP2018】【洛谷P5022】旅行【基环树】

基环树的第一道题。 NOIP2018 DAY2T1

stoorz 918
上一篇: 51Nod1022 石子归并V2
下一篇: Jzoj3542 冒泡排序
扩展的灰
博客等级 码龄12年 60粉丝 342原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址