FJUT 聪明的商人(树上倍增)题解
思路:求树上两点的距离,显然是dep[u] + dep[v] - 2 * dep[lca],用树上倍增去写。
代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const ll INF = 1e17;
using namespace std;
struct Edge{
int to, next;
}edge[maxn << ];
int fa[maxn][], dep[maxn], head[maxn], tot, maxf;
void addEdge(int u, int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs(int x, int pre, int deep){
dep[x] = deep;
fa[x][] = pre;
for(int i = ; i <= maxf; i++){
if(fa[x][i - ] != -){
fa[x][i] = fa[fa[x][i - ]][i - ];
}
else break;
}
for(int i = head[x]; i != -; i = edge[i].next){
int v = edge[i].to;
if(v != pre){
dfs(v, x, deep + );
}
}
}
int LCA(int u, int v){
if(dep[u] < dep[v]) swap(u, v);
int dis = dep[u] - dep[v];
for(int i = ; i <= maxf; i++){
if(( << i) & dis)
u = fa[u][i];
}
if(u == v) return u;
for(int i = maxf; i >= ; i--){
if(fa[u][i] != fa[v][i]){
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][];
} void init(){
maxf = ;
memset(head, -, sizeof(head));
memset(fa, -, sizeof(fa));
tot = ;
}
int main(){
int n, m, w; while(~scanf("%d%d%d", &n, &m, &w)){
init();
for(int i = ; i < n - ; i++){
int a, b;
scanf("%d%d", &a, &b);
addEdge(a, b);
addEdge(b, a);
}
dfs(, -, );
for(int i = ; i < m; i++){
int p, q, v;
scanf("%d%d%d", &p, &q, &v);
int lca = LCA(p , q);
int dis = dep[p] + dep[q] - * dep[lca];
if(v - dis * w >= ){
printf("%d\n", v - dis * w);
}
else{
printf("pass\n");
}
}
}
return ;
}
FJUT 聪明的商人(树上倍增)题解的更多相关文章
- 【bzoj4568】[Scoi2016]幸运数字 树上倍增+高斯消元动态维护线性基
题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征.一些旅行者希望游 ...
- Codevs 2370 小机房的树 LCA 树上倍增
题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子, ...
- NOIP2013 货车运输 (最大生成树+树上倍增LCA)
死磕一道题,中间发现倍增还是掌握的不熟 ,而且深刻理解:SB错误毁一生,憋了近2个小时才调对,不过还好一遍AC省了更多的事,不然我一定会疯掉的... 3287 货车运输 2013年NOIP全国联赛提高 ...
- HDU 4822 Tri-war(LCA树上倍增)(2013 Asia Regional Changchun)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow ...
- [树上倍增+二分答案][NOIP2012]运输计划
题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 公元 2044 年,人类进入了宇宙纪元 L 国有 nn 个星球,还有 n-1n−1 条双向航道,每条航道建立在两个星球之间,这 n-1n− ...
- 【codeforces666E】Forensic Examination 广义后缀自动机+树上倍增+线段树合并
题目描述 给出 $S$ 串和 $m$ 个 $T_i$ 串,$q$ 次询问,每次询问给出 $l$ .$r$ .$x$ .$y$ ,求 $S_{x...y}$ 在 $T_l,T_{l+1},...,T_r ...
- BNUOJ 52317 As Easy As Possible 树上倍增/主席树
题目链接: https://acm.bnu.edu.cn/v3/problem_show.php?pid=52317 As Easy As Possible Case Time Limit: 1000 ...
- 【bzoj3488】[ONTAK2010]Highways DFS序+树上倍增+树状数组
题目描述 一棵n个点的树,给定m条路径,q次询问包含一条路径的给定路径的个数+1 输入 The first line of input contains a single integer N(1< ...
- 【bzoj4009】[HNOI2015]接水果 DFS序+树上倍增+整体二分+树状数组
题目描述 给出一棵n个点的树,给定m条路径,每条路径有一个权值.q次询问求一个路径包含的所有给定路径中权值第k小的. 输入 第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数. 接下来n ...
随机推荐
- TensorFlow的封装
TensorFlow是比较底层的深度学习API,TF-Slim.TFLearn.Keras和TensorLayer均尝试简化TensorFlow/Theano,它们对TensorFlow/Theano ...
- 删除SQL Server大容量日志的方法(转)
删除SQL Server大容量日志的方法 亲自实践的方法 1.分享数据库,如果提示被其他连接占用,不能分离,刚勾上drop connections 2.复制下所有文件,一定要备份好,以防自己操作失误 ...
- 在lnmp1.3布置的web服务器上运行thinkphp3.2.3项目pathinfo路径模式
通过我的经历希望能给大家带来一些帮助: 我是在Linux系统上通过https://lnmp.org/install.html设置Nginx服务器,使用的是lnmp1.3版本,之后将一个thinkphp ...
- Python全栈-day12-day13-函数4
1.迭代器 1)定义:迭代取值的工具 2)优缺点 优点 a.提供一种不依赖索引取值的方法 b.同时一时刻内存在存在的值只有一个,更加省内存 缺点 a.取值麻烦,只能通过next方法一个一个地往后取 b ...
- [转]sql server 常用脚本(日常查询所需)
1:查看sql server代理中作业的运行状况的脚本 -- descr : a simple sql script to view sql server jobs run status -- las ...
- 排序(Sort)-----选择排序
声明:文中动画转载自https://blog.csdn.net/qq_34374664/article/details/79545940 1.选择排序简介 选择排序(Select Sort ...
- multiprocessing 源码解析 更新中......
一.参考链接 1.源码包下载·链接: https://pypi.org/search/?q=multiprocessing+ 2.源码包 链接:https://pan.baidu.com/s/1j ...
- python selenium设置chrome的下载路径
python可以通过ChromeOptions设置chrome参数,如下载路径等,代码如下(python 3.6.7): #-*-coding=utf-8-*- from selenium impor ...
- inux 驱动程序开发中输入子系统总共能产生哪些事件类型(EV_KEY,EV_ABS,EV_REL)
inux 驱动程序开发中, 输入子系统总共能产生哪些事件类型?,以及分别是什么意思?详见如下: Linux中输入设备的事件类型有EV_SYN 0x00 同步事件EV_KEY 0x01 按键事件,如KE ...
- 文件缓冲区在fork后复制
场景:父进程trace进程A,当A进程fork子进程B时,让父进程也fork子进程去trace子进程B,用于trace的进程将被trace的进程发生的系统调用号通过fprintf存入各自文件中 问题: ...