lightOJ——1094【树的直径】

避开PSRR仿真三大坑:用Cadence psspxf分析分频器时,这些设置错了白忙活 本文深入探讨了使用Cadence psspxf工具进行分频器PSRR仿真时常见的三大技术陷阱,包括扰动信号参数设置、收敛性优化和后处理信号提取。通过详细的参数推荐和实战技巧,帮助工程师避免仿真误差,提升PSRR分析效率与准确性,特别适用于16分频器等数字-模拟混合电路设计。 阅读详情

【题意】:

题目大概意思是给你一棵树,让你找树上最长距离的路径。两次 bfs ,就可以得到树的直径了。

The longest distance between two nodes in a tree is called the diameter of the tree. Tree is a connected graph with no cycles. The steps to find the diameter of a tree are:

  1. Select any node a and find the node x farthest from it.
  2. Again find the node y farthest from x. The distance between x and y is called the diameter of the tree.

For the following given scenarios, find the diameter of the trees.

Input Specification

There are T (≤ 10) test cases.

For each case, you will be given one integer n (2 ≤ n ≤ 30000) denoting the number of nodes in the tree. The nodes are 0-indexed (that means, they are numbered from 0 to n-1). The next n-1 lines will contain three integers p q r (0 ≤ p, q < n, p ≠ q, 1 ≤ r ≤ 10000) denoting that node p and q are connected by an edge whose weight is r.

Output Specification

Print the case number and the diameter.

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

Sample Output

Case 1: 100

Case 2: 80

ac代码:

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

const int maxn=3e4+10;

int dis[maxn],ans;
bool vis[maxn];
vector<pair<int ,int > >c[maxn];
int bfs(int x)
{
	memset(dis,0,sizeof(dis));
	memset(vis,0,sizeof(vis));
	queue<int >qe;
	qe.push(x);
	vis[x]=1;
	int p=0;
	while(!qe.empty()){
		int f=qe.front();
		qe.pop() ;
		if(dis[f]>ans){
			ans=dis[f];
			p=f;
		}
		pair<int ,int >t;
		for(int i=0;i<c[f].size();i++){
			t=c[f][i];
			if(vis[t.first]==0)
			{
				vis[t.first]=1;
				dis[t.first]=dis[f]+t.second;
				qe.push(t.first);
			}
		}
		
	}
	return p;
}
int main()
{
	int t,n,x,y,z,m=0;
	scanf("%d",&t);
	while(t--)
	{
		
		scanf("%d",&n);
		for(int i=0;i<n-1;i++){
			scanf("%d%d%d",&x,&y,&z);
			c[x].push_back(make_pair(y,z));
			c[y].push_back(make_pair(x,z));
		}
		ans=0;
		int k=bfs(0);
	//	printf("%d\n",k);
		ans=0;
		bfs(k);
		printf("Case %d: %d\n",++m,ans);
		for(int i=0;i<=n;i++)
			c[i].clear();
	}
	return 0;
}
 
 

 

《算法第四版》学习心得一——怎么学算法  一、算法的两个核心 算法的核心内容实际上有两个: 算法的概念和思路。 算法的编程实现。 两者相互关联影响,缺一不可。 不理解基本概念和思路,就算把实现代码背下来,也无济于事,因为脑袋里依然是一片模糊。理解了概念和思路,不去用代码实现,脑袋里有的只是概念,无法根据真实需求实现解决方案。 二、学算法需要什么基础 首先,必须要有一定的编程语言基础,要理解语言中最基本的概念:数据类型,类,成... 阅读详情

相关推荐

NSGA-Ⅱ算法:从原理到实战,解锁多目标优化难题

本文深入解析NSGA-Ⅱ算法在多目标优化中的应用,从原理到实战全面覆盖。通过快速非支配排序、精英策略和拥挤度比较三大创新,NSGA-Ⅱ有效解决了传统算法的效率与多样性问题。结合工业案例展示其在注塑工艺优化中的显著效果,为工程师提供实用解决方案。

weixin_29207149的博客 370

Lightoj 1094 【DFS】

题意: 给你一颗n-1条边的树,求某两点的最长路 思路: 随便找个根,然后我们从根往下搜,对于每个结点会出现多个子节点或者一个子节点或者没有结点, 多个子节点的话,就以他为中间点,在子节点中找两条最长的,加起来比比; 直接搜吧; 哎好难讲啊,其实画个图就很容易了;(直接注释在代码里面吧。。 #include <bits/stdc++.h> using ...

weixin_30588907的博客 158

告别迷茫!用ETAS RTA-OS配置你的第一个AUTOSAR OS任务(含VRTA虚拟ECU实战)

本文详细介绍了如何使用ETAS RTA-OS工具链在VRTA虚拟ECU上配置和运行第一个AUTOSAR OS任务。通过从XML配置到任务调度的全流程实战,帮助开发者快速掌握嵌入式操作系统开发的核心步骤,特别适合刚接触AUTOSAR的开发者快速上手。

weixin_42533622的博客 429

LIGHTOJ 1094树的直径

http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1094 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 w...

lanshan1111的博客 354

9、储备池学习:从学习到学习的新范式

本文探讨了基于学习到学习(L2L)框架的储备池计算新范式,提出通过在外循环中优化储备池的超参数(如递归连接权重、输入权重和读出初始化),以提升其在多种任务中的内循环学习性能。研究展示了两种主要实验设置:一是优化储备池用于回归二阶Volterra滤波器任务,显著优于随机基线;二是实现无需调整读出权重、仅依赖内部动态学习非线性函数的能力,表明储备池可快速构建目标函数的内部模型并安装任务先验知识。结合流程图与表格,文章系统呈现了L2L的训练流程、技术细节及实验结果,并讨论了性能提升、泛化能力等开放问题,指出了未来

ujm56789012的博客 513

小红书urlscheme和Activity

本文档详细介绍了小红书App的Deeplink跳转规范,涵盖了多个功能模块的跳转协议。主要内容包括:主页各Tab(关注、发现、购买)的跳转方式;不同类型笔记(图文/视频/长笔记)的详情页跳转参数;用户相关页面(主页/绑定/资料/消息等)的跳转协议;搜索功能(输入页/结果页)的跳转方式;以及话题、标签、专辑、H5页面等特殊场景的跳转实现。文档还提供了各跳转协议所需的参数说明,包括参数类型、默认值、是否必填等详细信息,并标注了不同版本和平台的兼容性要求。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 792

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

【Android逆向工程】第17章:反调试与反 Hook 对抗技术

本文介绍了Android应用常见的反调试技术及其对抗策略。主要内容包括: 反调试技术概述 分类:进程检测(ptrace、TracerPid)、文件检测、行为检测(时间/断点)、内存检测、框架检测 对抗思路:识别检测点→Hook检测函数→修改返回值→隐藏工具特征 ptrace检测实现 原理:利用ptrace系统调用检测调试器附加 实现方式:直接检测、持续线程检测、fork子进程检测 绕过方法:Frida Hook ptrace函数返回0,或使用LD_PRELOAD注入假函数 综合防护 涉及多种检测技术的组合使

w987333120的博客 362

【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的博客 475

基于STM32的智能快递柜设计(全部资料)

本文设计了一种基于STM32F407ZGT6的智能快递柜系统,通过模块化硬件设计和软件算法优化,实现了24小时无人值守、温湿度智能调控、远程监控等功能。系统集成OLED显示、GSM通信、舵机控制、超声波检测等模块,存取响应时间≤3秒,识别准确率达99.2%,较传统模式效率提升60%。测试表明系统运行稳定,用户满意度达92%,具有成本低、安全性高、可扩展性强等特点,为智慧物流末端配送提供了创新解决方案。未来可引入AI算法、太阳能供电等功能进一步优化系统性能。

3254968421的博客 599

环形平滑标签(Circular Smooth Label, CSL)技术详解

环形平滑标签(CSL)是一种优化算法,旨在解决旋转目标检测中角度预测的边界问题。通过将角度回归转化为分类任务,并利用窗口函数平滑标签分布,CSL有效处理了角度周期性的梯度突变问题。其核心原理包括角度离散化和平滑标签编码,通过高斯、矩形、三角等窗口函数生成概率分布。CSL在技术实现上涉及标签转换流程、模型结构调整和损失函数优化,显著提升了检测精度和稳定性。在DOTA数据集上的实验表明,CSL在mAP和角度误差方面优于传统方法。应用场景包括遥感图像分析、自动驾驶和工业质检,部署时需注意旋转NMS编译和ONNX导

浩瀚之水的专栏 645

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

DCM07-诊断服务EcuReset ($11 )的配置和实现

文章目录1 诊断服务EcuReset ($11 )1.1 概述1.2 诊断模式声明组1.3 代码实现1.4 配置参数 传送门 ==>> AutoSAR实战系列300讲「糖果Autosar」总目录 1 诊断服务EcuReset ($11 ) 1.1 概述 该服务实现了 ECU 内的复位功能,协议的具体理论讲解请参考「FlashBootloader03【电控单元复位服务($11)】」。在接收到UDS 服务 0x11 的请求时,DCM 模块应触发 ModeDeclarationGroupPrototy

2072

LightOJ-1094 求树直径,水

LightOJ 1094 题意:给出一棵树求直径。 总结:搜两次即可。先任意找一个点为根找到离它最远的点,再以这个点为根找离它最远的距离。 // LightOJ-1094 #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #i...

weixin_30542079的博客 134

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【lightoj-1094】树的直径(DFS)

链接:http://www.lightoj.com/volume_showproblem.php?problem=1094 题意: 一共n各节点编号0-n-1, 输入n-1条无向边代表u-v距离为w,求最远的两个点的距离(即树的直径)。 思路: 如果用最短路径来求,n<=30000是会超时的,正确做法是先随便从一个点开始深搜,搜到最远的节点一定是直径其中一个节点...

ayci54153的博客 180

visio安装包

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

【LightOJ】1094Farthest Nodes in a Tree(树的直径模板题目)

题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1094 题目大意:现在有N个点,N-1条边,现在给出你这些变,让你求出这个图中的最长距离 思路:树的直径的裸题了。 代码: #include&lt;stdio.h&gt; #include&lt;iostream&gt; #in...

红点雷龙XL 274

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LightOj 1094 Farthest Nodes in a Tree【树的直径】

题目: 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 ...

二喵君的博客 371

Lightoj 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

世界很大 1029

LightOJ 1094 Farthest Nodes in a Tree 树的直径

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

弱智就要多努力 397

(树的直径)LightOJ -- 1094

链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/C 求树的直径,这里只不过给每条边增加一个边长属性,变成了求树上两点距离最远为多少 树的直径,在今天比赛前我是没有接触过的, 队友说在学长的博客上看是用了两个bfs就可以了,可我看的题解是用了两个dfs 应该意思是一样的吧!!! 都学习...

weixin_34409822的博客 218

LightOJ1094Farthest Nodes in a Tree(BFS+树的直径)

Description 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

菜鸟的自我修养 419

【LightOJ】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

wyg1997 629

LightOj1094 Farthest Nodes in a Tree(树的直径:BFS模板)

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 w...

pxlsdz的博客 424

Lightoj 1094 - Farthest Nodes in a Tree 【树的直径裸题】

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/C ///#pragma comment (linker, "/STACK:102400000,102400000") #include #include #include #include #include #include #include

大太阳 918

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

树的直径:假设 s-t这条路径为树的直径,或者称为树上的最长路 现有结论,从任意一点u出发搜到的最远的点一定是s、t中的一点,然后在从这个最远点开始搜,就可以搜到另一个最长路的端点。即用两遍搜索就可以找出树的最长路。题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1094题意:树的节点间,路径间边的权值和最大是多少代码:#i

mfcheer 489

lightoj-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),

_Somethingwill 369
上一篇: POJ——【经典dp+记忆化搜索】滑雪
下一篇: HDU——2647【发奖金】
sf_yang35
博客等级 码龄8年 16粉丝 38原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值