Light OJ 1094 Farthest Nodes in a Tree (树的直径)

Vivado FPGA 工程模板:参数化消抖、PWM 与 XDC 约束 这份工程模板解决 FPGA 起步阶段的工程骨架与踩坑问题。含可综合的参数化按键消抖(两级同步消亚稳态)、参数化 PWM(0/满占空比边界修复)、顶层呼吸灯示例、XDC 时钟与 IO 约束模板、Tcl 一键建工程脚本。面向 STM32 开发者向 FPGA 扩展,换 Artix-7/Kintex/Zynq 只需改器件与引脚。 立即下载
1094 - Farthest Nodes in a Tree
Time Limit: 2 second(s)Memory Limit: 32 MB

Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000) denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.

Output

For each case, print the case number and the maximum distance.

Sample Input

Output for Sample Input

2

4

0 1 20

1 2 30

2 3 50

5

0 2 20

2 1 10

0 3 29

0 4 50

Case 1: 100

Case 2: 80

Notes

Dataset is huge, use faster i/o methods.

树的直径模板题

从任意一点u出发搜到的最远的点一定是s、t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点,即用两遍广搜就可以找出树的最长路 这个链接有它的证明http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 400010
using namespace std;
int edgesum=0;
int head[MAX*2]; 
int ans;
int T;
int dis[MAX*2];
bool vis[MAX*2];
struct node
{
	int from,to,val,next;
}edge[MAX*2];
void add(int u,int v,int w)//邻接表储存图 
{
	edge[edgesum].from=u;
	edge[edgesum].to=v;
	edge[edgesum].val=w;
	edge[edgesum].next=head[u];
	head[u]=edgesum++;
}
void BFS(int s)
{
	int i;
    queue<int>Q;
	memset(dis,0,sizeof(dis));
	memset(vis,false,sizeof(vis));
	vis[s]=true;
	dis[s]=0;
	Q.push(s);
	ans=0;
	while(!Q.empty())
	{
		int u=Q.front();
		Q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			int v=edge[i].to;
			if(!vis[v])
			{
				if(dis[v]<dis[u]+edge[i].val)
				{
					dis[v]=dis[u]+edge[i].val;
					if(ans<dis[v])
					{
						ans=dis[v];
						T=v;
					}	
				}
				vis[v]=true;
				Q.push(v);
			}
		}
	}
}
int main()
{
	int n,p,a,b,c;
	scanf("%d",&p);
	int l=1;
	while(p--)
	{
	    memset(head,-1,sizeof(head));//初始化头指针为-1,否则将陷入死循环 
		scanf("%d",&n);
		for(int i=1;i<n;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			add(a,b,c);//建图,因为是无向图,所以要双向见图 
			add(b,a,c);
		}
		BFS(0);//先任意找一点搜索,寻找距离他最远的点 ,即最长路的一个端点 
		BFS(T);//寻找最长路的另一个端点 
		printf("Case %d: %d\n",l++,ans);
	}
	return 0;
}


四旋翼无人机模糊自适应PID控制,俯仰姿态控制律设计研究(Matlab代码、Simulink仿真实现) 内容概要:本文围绕四旋翼无人机的俯仰姿态控制问题,深入研究了模糊自适应PID控制律的设计与实现。通过结合模糊逻辑系统的自适应能力与传统PID控制器的稳定性,构建了一种能够在复杂动态环境中有效应对外部扰动和模型不确定性的先进控制策略。研究详细阐述了控制系统的结构设计、模糊规则库的建立、隶属度函数的选取以及PID参数的在线调整机制,并利用Matlab/Simulink工具进行了完整的建模与仿真实验,验证了所提方法在响应速度、超调量和抗干扰能力等方面相较于传统PID控制的显著优越性。; 适合人群:具备自动控制理论基础和Matlab/Simulink仿真经验的自动化、航空航天及相关专业的本科生、研究生及科研人员。; 使用场景及目标:①学习和掌握模糊自适应PID控制器的设计原理与方法;②研究无人机姿态控制算法的性能提升途径;③完成相关课程设计、毕业设计或科研项目的仿真验证工作。; 阅读建议:建议读者结合Matlab代码与Simulink模型,深入理解控制算法的实现细节,并尝试调整系统参数和仿真条件,以探究不同因素对控制性能的影响,从而达到理论与实践相结合的学习目的。 立即下载

相关推荐

复现基于李雅普诺夫的多个欠驱动无人船协同路径跟踪Matlab仿真

内容概要:本文详细介绍了基于李雅普诺夫稳定性理论的多个欠驱动无人船协同路径跟踪控制方法的Matlab仿真研究。通过建立非线性动力学模型,设计分布式协同控制器,并利用李雅普诺夫函数严格证明系统的渐近稳定性,实现了多艘无人船在复杂海洋环境下的高精度路径跟踪与稳定编队保持。文中系统阐述了控制器的设计原理、稳定性分析过程、仿真参数配置及多种典型场景下的仿真实验结果,充分验证了该方法在提升编队协同性、抗外部干扰能力和鲁棒性方面的显著优势。; 适合人群:具备自动控制理论、非线性系统分析基础以及Matlab/Simulink仿真能力的研究生、科研人员,以及从事无人系统、船舶自动化、多智能体协同控制等领域研发工作的工程师。; 使用场景及目标:①用于多智能体系统协同控制算法的研究与教学示范;②为欠驱动船舶的路径跟踪控制、海上集群航行等实际工程应用提供可靠的算法设计思路与仿真验证平台;③支撑相关领域高水平论文撰写、科研项目申报与关键技术攻关。; 阅读建议:建议读者结合提供的Matlab代码逐模块研读,重点关注李雅普诺夫函数的构造技巧与控制器参数的整定方法,并尝试在不同环境扰动(如海流、风浪)下进行仿真实验,以深入理解系统的鲁棒性与适应性。

Light oj 1094 Farthest Nodes in a Tree(树的最大直径)

1094 - Farthest Nodes in a Tree     PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles

Shyazhut 368

Cursor 使用教程(2025年更新版)

官方Cursor详细使用教程在“扩展学习”

我是赛博AI Lewis 7088

【Light-oj】-1094 - Farthest Nodes in a Tree(树的直径)

1094 - Farthest Nodes in a Tree     PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles),

Bear1998的博客 476

Ubuntu 22.04下GNU Radio 3.10全家桶编译避坑实录:从UHD、VOLK到WiFi收发模块gr-ieee802-11

本文详细介绍了在Ubuntu 22.04系统下编译安装GNU Radio 3.10全家桶的完整流程,包括UHD驱动、VOLK优化库以及WiFi信号处理模块gr-ieee802-11的集成。通过逐步指导解决版本兼容性和依赖问题,帮助研究人员快速搭建高效的软件定义无线电开发环境,特别适合使用USRP设备进行无线通信研究的开发者。

weixin_30825199的博客 452

某二级支行网络的设计与实现——配置脚本(1)

【代码】某二级支行网络的设计与实现——配置脚本(1)

u010311846的博客 589

Light OJ 1094 Farthest Nodes in a Tree(树的直径模板)

Farthest Nodes in a Tree Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Given a tree (a connected graph with no cycles), you h

※夏日星空※ 280

踩坑记录:服务商模式下微信小程序支付,为什么必须用子商户的AppID获取openid?

本文深入解析了服务商模式下微信小程序支付必须使用子商户AppID获取openid的技术原因,详细对比了服务商模式与直连模式的关键差异,并提供了正确的配置指南和常见问题解决方案,帮助开发者避免微信支付集成中的常见陷阱。

weixin_28318011的博客 553

Light OJ 1094 - Farthest Nodes in a Tree【树的直径 两次bfs】

1094 - Farthest Nodes in a Tree     PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles),

wyjwyl的专栏 583

保姆级教程:在mmdetection中手动计算并可视化P、R、F1值(附完整代码)

本文提供了一份详细的mmdetection教程,指导开发者如何手动计算并可视化精确率(P)、召回率(R)和F1值。通过完整的代码示例和清晰的步骤说明,帮助用户深入理解目标检测模型的性能评估方法,特别适合需要精细分析模型表现的计算机视觉研究人员和工程师。

weixin_29038303的博客 404

保姆级教程:用DaVinci Configurator & Developer从零搭建AUTOSAR OS(TC2xx三核实战)

本文提供了一份详细的AUTOSAR OS配置指南,使用DaVinci Configurator和Developer工具从零开始搭建Infineon TC2xx三核系统。涵盖多核配置协同逻辑、版本兼容性陷阱及验证错误的解决方案,帮助开发者在3小时内构建可运行的多核OS框架。

weixin_33940469的博客 324

Light oj 1094 - Farthest Nodes in a Tree【树的直径】

1094 - Farthest Nodes in a Tree     PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles),

希望的专栏 660

YOLOv12超参数智能优化:基于内置Ray Tune的自动化调优实战指南

购买即可解锁300+YOLO优化文章,并且还有海量深度学习复现项目,价格仅需两杯奶茶的钱,别人有的本专栏也有! 目标检测模型的性能瓶颈往往源于超参数配置不当。传统手动调优方式不仅耗时耗力,且难以找到全局最优解。YOLOv12集成的Ray Tune框架为解决这一难题提供了完整方案,通过分布式异步优化算法,可实现超参数搜索效率的指数级提升。实际测试表明,采用Ray Tune自动调优的YOLOv12模型,在COCO数据集上mAP指标平均提升3.8%,收敛速度加快2.1倍。在VisDrone无人机数据集上的跨域测试

LXY_LOVE36的博客 122

Light-oj-1094 Farthest Nodes in a Tree (树的直径模板题)

Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Description Given a tree (a connected graph with no cycles), you have to fin

lu_1110的博客 326

RK3568平台(音频篇)麦克风PDM接口录音

PDM = Pulse Density Modulation是一种用数字信号表示模拟信号的调制方法。PDM则使用远高于PCM采样率的时钟采样调制模拟分量,只有1位输出,要么为0,要么为1。因此通过PDM方式表示的数字音频也被称为Oversampled 1-bit Audio。相比PDM一连串的0和1,PCM的量化结果更为直观简单。以PDM方式作为模数转换的接收端,需要用到抽取滤波器(Decimation Filter),将密密麻麻的0和1代表的密度分量转换为幅值分量,而PCM方式得到的已经是幅值分量了。

weixin_49303682的博客 5199

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

light oj 1094 Farthest Nodes in a Tree(树的直径模板)

1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles), yo

莫道青天真无道,能逍遥时且逍遥! 901

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

light oj 1094 Farthest Nodes in a Tree(树的直径模板)

Farthest Nodes in a Tree Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Given a tree (a connected graph with no cycles), you have to

昨日明眸 310

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Light OJ:1094 Farthest Nodes in a Tree(树状DP+统计树的最大直径)

1094 - Farthest Nodes in a Tree     PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles),

zugofn的博客 383

Light OJ 1094 - Farthest Nodes in a Tree

Light OJ 1094 - Farthest Nodes in a Tree 传送门 思路:树的直径。 方法1:两次bfsbfsbfs,第一次找到直径的一个端点,然后再从该端点bfsbfsbfs一次的最大距离即为直径,bfsbfsbfs方法需要用一个数组ddd维护距离和判断是否在队列中。 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=3e4+5,M=1e6+5,inf=0x3f3

Harris-H的博客 597

【Light oj 1094 Farthest Nodes in a Tree】

Farthest Nodes in a TreeDescription Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you

楚江枫 622

LightOJ 1094 Farthest Nodes in a Tree 树的直径

题目:http://www.lightoj.com/volume_showproblem.php?problem=1094 题意:给定一个由n个点的树,树边都有权值,求树中相距最远的两个点的距离是多少 思路:此题求树的直径。首先任选一点做dfs,求出此点到其他点的距离,然后从距离最远的点(此点为树的直径的一个端点)开始再做一次dfs,求出到所有点的距离,此时的最大距离就是树上两点间的最大距离,

弱智就要多努力 397

Light OJ 1094 + Farthest Nodes in a Tree(搜索)

大致题意:给出一个n个节点的无环连通图, 每条边都有权值,求最长的两个点之间的距离。 #include #include #include #include #include using namespace std; const int M = 30010; struct edge{ int to; int val; edge(){} edge(int pp, int qq):

u011867401的专栏 481

LightOJ 1094-1094 - Farthest Nodes in a Tree【树的直径模板】

1094 - Farthest Nodes in a Tree     PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given a tree (a connected graph with no cycles),

小I的博客 491
上一篇: Light OJ 1349 Aladdin and the Optimal Invitation
下一篇: POJ 1985 Cow Marathon (树的直径)
Calm微笑
博客等级 码龄11年 196粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值