LCA
How far away ?
Problem Description
There are houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
Input
First line is a single integer , indicating the number of test cases.
For each test case,in the first line there are two numbers and ,the number of houses and the number of queries. The following lines each consisting three numbers , separated bu a single space, meaning that there is a road connecting house and house ,with length .The houses are labeled from to .
Next lines each has distinct integers and , you areato answer the distance between house and house .
Output
For each test case,output lines. Each line represents the answer of the query. Output a bland line after each test case.
Sample Input
Sample Output
Source
题目链接
题目描述
给定一个生成树,求任意两个结点的距离
解题思路
- 时间复杂度:
2.tarjan优化
本质:一种离线做法,并查集对“向上标记法”的优化
- 时间复杂度:
代码
- 常规代码
- 高效代码
__EOF__

本文链接:https://www.cnblogs.com/zyyun/p/15139481.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!