当前位置:网站首页>codevs 2370 小机房的树 (LCA)
codevs 2370 小机房的树 (LCA)
2022-08-10 11:07: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 问题,我们为每个点设置一个权值 tim[i]
等于从根节点到其路径中所有边权的和,于是虫子所要爬行的最短路径权值为 tim[u]+tim[v]-2*tim[lca(u,v)]
。
采用离线 tarjan 算法可以解决这一问题,每条边的权值我们可以将其保存在子节点中。
不过缺点就是离线算法不能保证计算顺序与输入顺序的一致,因此我们采用一个变量来标记每个查询的序号,得出所有结果后根据该序号排序后输出。
AC 代码
边栏推荐
- Stroke Practice - 62 Valid Sudokus
- Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
- 模块九 - 设计电商秒杀系统
- 【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
- Short video software development - how to break the platform homogenization
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- 【勇敢饭饭,不怕刷题之链表】有序链表的合并
- The impact of development mode on testing
- 杭电多校-Loop-(不确定性贪心+线段树)
- 三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
猜你喜欢
Memory problems difficult to locate, it is because you do not use ASAN
Several small projects that I have open sourced over the years
中小规模网站架构
1-IMU参数解析以及选择
从源码角度分析UUID的实现原理
老板加薪!看我做的WPF Loading!!!
微信小程序提交审核历史版本记录从哪里查看
一文带你搞懂中断按键驱动程序之poll机制
Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
石墨文档打开文档时快速定位到上次写的位置
随机推荐
SQL优化最强总结 (建议收藏~)
三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
HCIP ---- VLAN
POJ 1026 Cipher (置换群)
微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
从脚本到剪辑,影像大师亲授的后期制作秘籍
JWT implements login authentication + Token automatic renewal scheme
2023版揽胜运动曝光,安全、舒适一个不落
单目操作符(含原码反码补码转换)
Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
CodeChef STMRRG String Merging (dp)
4 of huawei offer levels, incredibly side is easing the bit in the interview ali?
POJ 3101 Astronomy (数学)
From the product dimension, why can't we fully trust Layer2?
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
Licking Exercise - 63 Find all anagrams in a string
Module 9 - Designing an e-commerce seckill system