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

节卡Jaka Zu APP编程避坑指南:从频繁通讯错误到稳定Socket交互的调试心得 本文详细解析了节卡Jaka Zu APP编程中常见的通讯错误问题,并提供了从基础排查到高级调试的完整解决方案。通过优化网络配置、固件匹配、心跳机制和数据交互规范,开发者可实现稳定的Socket通讯,有效提升工业机器人操控的可靠性。文章特别针对v1.6.7.3版本的典型问题给出了实用调试技巧。 阅读详情

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-1 lines 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.


树的直径的裸题,详解看:点击打开题目


#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define MAX 40000
int n;
vector<int> link[MAX+10];
vector<int> va[MAX+10]; 
bool vis[MAX+10];
int dis[MAX+10];
void init()
{
	for(int i=0;i<n;i++)
	{
		link[i].clear();
		va[i].clear();
	}
}
int bfs(int x)
{
	int ans;
	int maxx=0;
	memset(vis,false,sizeof(vis));
	memset(dis,0,sizeof(dis));
	queue<int> q;
	dis[x]=0;
	q.push(x);
	vis[x]=true;
	while(!q.empty())
	{
		int st=q.front();
		q.pop();
		for(int i=0;i<link[st].size();i++)
		{
			if(!vis[link[st][i]])
			{
				q.push(link[st][i]);
				dis[link[st][i]]=dis[st]+va[st][i];
				if(maxx<dis[link[st][i]])
				{
					maxx=dis[link[st][i]];
					ans=link[st][i];
				}
				vis[link[st][i]]=true;
			}
		}
	}
	return ans;
}
int main()
{
	int u,cas=1;
	scanf("%d",&u);
	while(u--)
	{
		init();
		scanf("%d",&n);
		for(int i=1;i<n;i++)
		{	
			int t1,t2,t3;
			scanf("%d %d %d",&t1,&t2,&t3);
			link[t1].push_back(t2);
			va[t1].push_back(t3);
			link[t2].push_back(t1);
			va[t2].push_back(t3);
		}
		int z=bfs(0);
		int ans=bfs(z);
		printf("Case %d: %d\n",cas++,dis[ans]);	
	}
	return 0;
}


40.8K星爆火!Crawl4AI实战突破Cloudflare 5秒盾:AI反爬新范式,爬虫再也不被封 Crawl4AI的爆火(40.8K星)不是偶然——它解决了爬虫工程师最头疼的反爬问题,让爬虫从“体力活”变成“脑力活”,不用再花费大量时间配置指纹、编写行为脚本。但要注意:爬虫必须遵守目标网站的robots.txt协议,不得用于非法爬取数据(比如用户隐私、商业机密)。合理使用技术,才能长久落地。如果你在使用Crawl4AI时遇到具体问题(比如绕过更严格的反爬、批量爬取配置),欢迎在评论区交流,我会第一时间分享解决方案! 阅读详情

相关推荐

别再重启了!Win11开机卡死/间歇性卡顿的终极排查与修复指南(附免费工具)

本文提供Win11开机卡死和间歇性卡顿的终极排查与修复指南,涵盖从后台进程清理到驱动优化、系统组件修复及硬件监控的全方位解决方案。通过专业工具和详细步骤,帮助用户彻底解决Win11卡顿问题,无需重启或重装系统。

weixin_31102231的博客 777

lightOJ——1094【树的直径】

【题意】: 题目大概意思是给你一棵树,让你找树上最长距离的路径。两次 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 t...

sf_yang35的博客 282

zynq+LWIP 裸机双网口实现(MIO+EMIO)+程序下载

芯片型号: ZYNQ: XC7Z010clg400开发软件:Vivado2022 + Xilinx Vitis 2022网卡芯片:RTL8211FDI(需要根据网口芯片型号修改对应的硬件配置代码。

thinking in 小宇宙 3422

POJ1985Cow Marathon[树的直径]

Cow Marathon Time Limit:2000MS Memory Limit:30000K Total Submissions:5117 Accepted:2492 Case Time Limit:1000MS Description After hearing about the epidemic of...

weixin_30797199的博客 305

机器学习面试笔试求职必背!八股文(1/5)

算法工程师,技术软件类求职机器学习必背八股文 更多算法/业务/HR面等笔试题面试题 —— >个性签名自取! 1. 特征工程: 1.1 为什么要对特征做归一化处理: Feature scaling,常见的提法有“特征归一化”、“标准化”,是数据预处理中的重要技术。特征间的单位(尺度)可能不同,比如身高和体重,比如摄氏度和华氏度,比如房屋面积和房间数,一个特征的变化范围可能是[ 1000 , 10000 ] [1000, 10000][1000,10000],另一个特征的变化范围可能是[ −.

大厂offer宝典 1万+

通讯接口应用笔记3:使用W5500实现Modbus TCP服务器

  前面我们设计实现了W5500的驱动程序,也讲解了驱动的使用方式。在最近一次的项目应用中,正好有一个使用W5500实现TCP通讯的需求,所以我们就使用该驱动程序轻松实现。这一篇中我们就来说一说基于我们W5500通讯驱动程序实现TCP通讯的过程。 1、应用需求   在本次应用中,要求实现一个基于W5500的Modbus TCP服务器。这个需求的描述虽然只有一句话,但是这个需求的内容可不简单。我们首先来分析一下这个需求的具体内容。   为了实现基于W5500的Modbus TCP服务器,我们必先须基于W550

木南创智 5504

【light oj】树的直径

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

~★sunshine☆的博客专栏 369

AWD线下攻防赛总结

在本地组策略编辑器里面,依次展开计算机配置—>管理模板—>Windows组件—>远程桌面服务—>远程桌面会话主机—>连接—>限制连接的数量。8.Web目录环境查找相关可疑文件:jpg/png/rar,属性、解压。进程线程:netstat / ps -aux/netstat -apt。4.查看/etc/init.d/rc.local中启动服务有无异常。1.不使用默认口令,修改成复杂的,并确保和web环境连接。5.使用脚本开启进程监控、目录监控、流量监控。文件操作监控.exe/御剑文件监控.exe。

XuJL89的博客 638

Lightoj 1094 【DFS】

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

weixin_30588907的博客 158

YOLOv11改进策略【Neck】| PRCV 2023,SBA(Selective Boundary Aggregation):特征融合模块,描绘物体轮廓重新校准物体位置,解决边界模糊问题

SBA 模块在医疗图像分割中具有重要作用,其设计出发点是解决图像中物体边界模糊以及融合特征时的冗余和不一致问题。

Limiiiing的博客 1953

2026美赛数学建模竞赛 MCM C题:与星共舞的数据 问题一 粉丝投票估计模型 详细解题思路和代码

2026美赛数学建模竞赛 MCM C题:与星共舞的数据 问题一 粉丝投票估计模型 详细解题思路和代码.。开发一个(或多个)数学模型,用于估算每位选手在他们参赛的各周的粉丝投票数(这是未知且严格保密的)。您的模型是否能正确估算出导致每周淘汰结果一致的粉丝投票数?提供一致性度量。您估算出的粉丝投票总数有多少确定性?这种确定性对于每位选手/每周是否总是相同?为您的估算提供确定性度量。

YOLO 856

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

YOLOv5_DeepSORT_Pytorch训练自己的多目标跟踪模型

1 准备 环境配置:https://blog.csdn.net/weixin_50008473/article/details/115250986?spm=1001.2014.3001.5501 YOLOv5_DeepSORT_Pytorch代码地址: https://github.com/mikel-brostrom/Yolov5_DeepSort_Pytorch DeepSORT论文:https://arxiv.org/pdf/1703.07402.pdf 2 训练目标检测模型 训练自己的目标检测模

weixin_50008473的博客 1万+

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

Calm微笑 408

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

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

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

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

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

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

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

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

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

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

LightOJ 1094 Farthest Nodes in a Tree 树的直径

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

弱智就要多努力 397

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
上一篇: 【POJ】-1985-Cow Marathon(树的直径)
下一篇: 【POJ】-2377-Bad Cowtractors(最小生成树)
Bear1997
博客等级 码龄11年 55粉丝 369原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值