当前位置:网站首页>codevs 2370 Small room tree (LCA)
codevs 2370 Small room tree (LCA)
2022-08-10 11:52:00 【51CTO】
Description
小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子,他们不想花费太多精力.已知从某个节点爬到其父亲节点要花费 c 的能量(从父亲节点爬到此节点也相同),他们想找出一条花费精力最短的路,以使得搞基的时候精力旺盛,他们找到你要你设计一个程序来找到这条路,要求你告诉他们最少需要花费多少精力
Input Description
第一行一个n,接下来n-1行每一行有三个整数u,v, c .表示节点 u 爬到节点 v 需要花费 c 的精力.(1<=n<=50000, 1<=m<=75000, 0<=c<=1000)
第n+1行有一个整数m表示有m次询问.接下来m行每一行有两个整数 u ,v 表示两只虫子所在的节点
Output Description
一共有m行,每一行一个整数,表示对于该次询问所得出的最短距离.
Sample Input
Sample Output
思路
LCA 问题,We set a weight for each point tim[i]
is equal to the sum of the weights of all edges in the path from the root node to it,So the shortest path weight to be crawled by the bug is tim[u]+tim[v]-2*tim[lca(u,v)]
.
采用离线 tarjan 算法可以解决这一问题,The weight of each edge we can store in the child nodes.
However, the disadvantage is that the offline algorithm cannot guarantee that the calculation order is consistent with the input order,So we use a variable to label the sequence number of each query,After all the results are obtained, they are sorted according to the sequence number and output.
AC 代码
边栏推荐
- Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
- VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
- Licking Exercise - 58 Verifying Binary Search Trees
- 三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
- 【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
- AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
- A little self-deprecating deconstruction about farmers "code"
- LeetCode_443_压缩字符串
- 蔚来-软件开发工程师一面记录
- 力扣练习——63 找到字符串中所有字母异位词
猜你喜欢
随机推荐
Introduction to Software Architecture
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
网络套接字(UDP和TCP编程)
做自媒体月入几万?博主们都在用的几个自媒体工具
态路小课堂丨如何为CXP光模块选择光纤跳线?
Nocalhost - Making development more efficient in the cloud-native era
【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
CPU多级缓存与缓存一致性
Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
中小规模网站架构
3款不同类型的自媒体免费工具,有效提高创作、运营效率
微信小程序提交审核历史版本记录从哪里查看
【小程序 | 启航篇】一文打通任督二脉
有哪些好用的性能测试工具推荐?性能测试报告收费标准
Weilai-software development engineer side record
【无标题】
Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
基于UiAutomator2+PageObject模式开展APP自动化测试实战
使用.NET简单实现一个Redis的高性能克隆版(六)
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决