当前位置:网站首页>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 代码
边栏推荐
- Chapter 22 Source Code File REST API Reference (4)
- Centos7环境使用Mysql离线安装包安装Mysql5.7
- 快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
- Module 9 - Designing an e-commerce seckill system
- AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
- VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
- 模块九 - 设计电商秒杀系统
- 【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
- LCD驱动端与设备端名称匹配过程分析(Tiny4412)
- 使用.NET简单实现一个Redis的高性能克隆版(六)
猜你喜欢

AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
![[E-commerce operation] Do you really understand social media marketing (SMM)?](/img/5b/6682c613305deb3dc15401077d38a0.png)
[E-commerce operation] Do you really understand social media marketing (SMM)?

Where can I view the version record of WeChat applet submission review history?

关于振弦采集模块及采集仪振弦频率值准确率的问题

【勇敢饭饭,不怕刷题之链表】链表中有环的问题

2022年裁员潮,失业程序员何去何从?

推荐6个自媒体领域,轻松易上手

从源码角度分析UUID的实现原理

VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决

Module 9 - Designing an e-commerce seckill system
随机推荐
LeetCode_443_压缩字符串
推荐6个自媒体领域,轻松易上手
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
[E-commerce operation] Do you really understand social media marketing (SMM)?
蔚来-软件开发工程师一面记录
LeetCode 21. 合并两个有序链表
杭电多校-Loop-(不确定性贪心+线段树)
老板加薪!看我做的WPF Loading!!!
【机器学习】浅谈正规方程法&梯度下降
做自媒体月入几万?博主们都在用的几个自媒体工具
力扣练习——62 有效的数独
HDU 6040 Hints of sd0061 (技巧)
10 个 Reduce 常用“奇技淫巧”
负载均衡原理分析与源码解读
Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
中小规模网站架构
Some tips for using Unsafe
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
暑期总结4
LeetCode 445. 两数相加 II