Luogu 2912 [USACO08OCT]牧场散步Pasture Walking
快乐树剖
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
#define lson nd << 1
#define rson nd << 1 | 1
using namespace std; const int N = 1e4; int dep[N], top[N], f[N], son[N], size[N], id[N];
int q, n, head[N], tot, cnt, A[N], a[N];
int sum[N << ]; struct edge {
int nxt, to, val;
}e[N << ]; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} void added(int u, int v, int val) {
e[++tot].to = v;
e[tot].nxt = head[u];
e[tot].val = val;
head[u] = tot;
} void add(int u, int v, int val) {
added(u, v, val);
added(v, u, val);
} void dfs1(int u) {
size[u] = ;
for(int i = head[u]; i; i = e[i].nxt) {
int nt = e[i].to;
if(nt == f[u]) continue;
f[nt] = u; dep[nt] = dep[u] + ;
a[nt] = e[i].val;
dfs1(nt);
size[u] += size[nt];
if(size[nt] > size[son[u]]) son[u] = nt;
}
} void dfs2(int u) {
id[u] = ++cnt;
A[cnt] = a[u];
if(!son[u]) return;
top[son[u]] = top[u];
dfs2(son[u]);
for(int i = head[u]; i; i = e[i].nxt) {
int nt = e[i].to;
if(nt == f[u] || nt == son[u]) continue;
top[nt] = nt;
dfs2(nt);
}
} void update(int nd) {
sum[nd] = sum[lson] + sum[rson];
} void build(int l, int r, int nd) {
if(l == r) { sum[nd] = A[l]; return;}
int mid = (l + r) >> ;
build(l, mid, lson);
build(mid + , r, rson);
update(nd);
} int trie_sum(int L, int R, int l, int r, int nd) {
if(L <= l && r <= R) return sum[nd];
int tmp = , mid = (l + r) >> ;
if(L <= mid) tmp += trie_sum(L, R, l, mid, lson);
if(mid < R) tmp += trie_sum(L, R, mid + , r, rson);
return tmp;
} void sw(int &x, int &y) {
x ^= y, y ^= x, x ^= y;
} int query(int x, int y) {
int ans = , tmp;
for(; top[x] != top[y];) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
tmp = trie_sum(id[top[x]], id[x], , n, );
ans += tmp;
x = f[top[x]];
}
if(dep[x] < dep[y]) swap(x, y);
tmp = trie_sum(id[y], id[x], , n, );
ans += tmp - a[y];
return ans;
} int main()
{
n = rd; q = rd;
for(int i = ; i < n; ++i) {
int x = rd, y = rd, z = rd;
add(x, y, z);
}
dfs1(); dfs2();
build(, n, );
for(; q; q--) {
int x = rd, y = rd;
printf("%d\n", query(x, y));
}
}
Luogu 2912 [USACO08OCT]牧场散步Pasture Walking的更多相关文章
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)
P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...
- 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]
P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...
- BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...
- [USACO08OCT]牧场散步Pasture Walking BZOJ1602 LCA
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- 洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- 洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- [luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)
传送门 水题. 直接倍增求lca. x到y的距离为dis[x] + dis[y] - 2 * dis[lca(x, y)] ——代码 #include <cstdio> #include ...
随机推荐
- php 学习笔记 设计和管理
代码管理 文件路径.数据库名.密码禁止 hard coded 避免重复代码在多个页面复制粘贴 Gang of Four eXtreme Programming 的主要原则是坚决主张测试是项目成功的关键 ...
- python之路之装饰器
一 装饰器进化之路1) import time def index(): start_time=time.time() time.sleep() print('welcome to index wor ...
- bootstrapValidator针对设置赋值进行验证
bootstrapValidator在提交的时候可以进行验证,但是对于点击输入框进行赋值的时候验证失效. 解决方法: 然后在设置change方法方可解决.
- win10/win7 笔记本 开启虚拟无线 批处理
Microsoft Virtual WiFi Miniport Adapter 一.看网络网卡 有多出的这一项“Microsoft Virtual WiFi Miniport Adapter”,那么说 ...
- JSON解析工具比较,主要GSON和FastJSON
JSON解析工具比较,主要GSON和FastJSON 一 .各个JSON技术的简介和优劣 1.json-lib json-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确 ...
- python模块说明
1.模块(变量,函数,类,实现一个功能) 包:用来从逻辑上组织模块,本质是一个目录(必须带有__init__.py) 2.导入方法 import module_alex from module_ale ...
- python 2 类与对象
1.类与对象的概念 类即类别.种类,是面向对象设计最重要的概念,从一小节我们得知对象是特征与技能的结合体,而类则是一系列对象相似的特征与技能的结合体. 那么问题来了,先有的一个个具体存在的对象(比如一 ...
- js前台遍历后台返回的Datatable数据
jsondata 后台返回的datatable ) { ; j < jsondata.length; j++) { jsondata[j]; }; }
- ubuntu16.04 dpkg强制安装 teamviewer
dpkg遇到安装有依赖,而依赖的包有无法安装的时候,可以试试强制安装: .90154_amd64.deb 虽然报错,但是安装后还是可以使用. 如果使用: .90154_amd64.deb 提示下面错误 ...
- UGUI RectTransform
RectTransform解析 当 Anchor 在同一点时,显示的是物体的座标与大小Pos X.Pos Y.Width.Height ,当 Anchor 不在同一点时(此时会形成矩形),显示的会是 ...