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 ...
随机推荐
- 并发基础(九) java线程的终止与中断
1.简单了解一下:为何不赞成使用 Thread.stop.Thread.suspend 和 Thread.resume? suspend .resume.stop方法分别完成了线程的暂停.恢复.终 ...
- PyQt
知识内容: 1. 2. 3. 以后有时间再写...
- flask高阶
内容: 1.进程线程复习 2.flask多线程的问题 3.线程隔离 4.LocalStack 5.flask上下文整理 6.多app应用 1.进程线程复习 (1)进程 进程是一个具有一定独立功能的程序 ...
- centos中YUM安装后文件的常见路径
1 php的相关 1)ini的文件 /etc/php.ini 2 apache相关 1) conf的文件 /etc/httpd/conf 2)错误日志 /etc/httpd/logs 3)扩展文 ...
- thymeleaf的使用
1.导包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- Linux&Unix命令
Linux下: 系统操作 文件操作 防火墙 权限管理 压缩和解压 安装应用 用户管理 端口 PID 应用 start & shutdown 远程操作 异常 注意点: linux系统下内容大多用 ...
- Mongodb 折腾笔记
简介: Mongodb 是一个由 C++ 语言编写的基于分布式文件存储的数据库,是目前最像关系型数据库的非关系型数据库. 下载地址:https://fastdl.mongodb.org/linux/m ...
- Eclipse json文件报错
只要找一个json在线解析,验证你的json文件格式的正确性,错误可以忽略. 如要消除红叉,关闭Json Validation即可,如下操作: Window > Preferences > ...
- 关于HTTP协议及SOCKET通信
一.HTTP 1.报文结构 HTTP:超文本传输协议,报文分为请求报文和响应报文. 2.端口(tomcat端口) http在熟知的80端口使用TCP的服务:tomcat的默认端口是8080 3.状态码 ...
- MongoDB 数据查询
数据查询 基本查询 方法find():查询 db.集合名称.find({条件文档}) 方法findOne():查询,只返回第一个 db.集合名称.findOne({条件文档}) 方法pretty(): ...