当前位置:网站首页>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 代码
边栏推荐
猜你喜欢
Some tips for using Unsafe
2022年裁员潮,失业程序员何去何从?
项目部署、
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
Memory problems difficult to locate, it is because you do not use ASAN
常量及数据类型你还记得多少?
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
模块九 - 设计电商秒杀系统
StoneDB Document Bug Hunting Season 1
[E-commerce operation] Do you really understand social media marketing (SMM)?
随机推荐
POJ 1026 Cipher (置换群)
Since the media hot style title how to write?Taught you how to write the title
CPU多级缓存与缓存一致性
APP automation testing practice based on UiAutomator2+PageObject mode
怎么加入自媒体,了解这5种变现模式,让账号快速变现
从源码角度分析UUID的实现原理
10 个 Reduce 常用“奇技淫巧”
Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
力扣练习——64 最长和谐子序列
三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
The brave rice rice, does not fear the brush list of 】 list has a ring
2022年裁员潮,失业程序员何去何从?
网络套接字(UDP和TCP编程)
不止跑路,拯救误操作rm -rf /*的小伙儿
项目部署、
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
Centos7环境使用Mysql离线安装包安装Mysql5.7
Programmers pursue technology to consolidate basic learning route suggestions
不止跑路,拯救误操作rm -rf /*的小伙儿
OSSCore 开源解决方案介绍