快乐树剖

 #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的更多相关文章

  1. LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...

  2. luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  3. bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)

    P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...

  4. 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]

    P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...

  5. 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 ...

  6. [USACO08OCT]牧场散步Pasture Walking BZOJ1602 LCA

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  7. 洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  8. 洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  9. [luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)

    传送门 水题. 直接倍增求lca. x到y的距离为dis[x] + dis[y] - 2 * dis[lca(x, y)] ——代码 #include <cstdio> #include ...

随机推荐

  1. 大话java性能优化 pdf 下载(全本)

    扫加公众号,回复”大话java性能优化",免费获取此书.

  2. sqlserver操作命令

    启动命令:Net Start MSSqlServer 暂停命令:Net Pause MSSqlServer 重新启动暂停的命令:Net Continue MSSqlServer 停止命令:Net st ...

  3. Java 知识点(转)

    1.servlet执行流程 客户端发出http请求,web服务器将请求转发到servlet容器,servlet容器解析url并根据web.xml找到相对应的servlet,并将request.resp ...

  4. c++官方文档-class

    #include <iostream> using namespace std; class Circle { double radius; public: Circle(double r ...

  5. 管道| , <<<重定向

    https://blog.csdn.net/stormbjm/article/details/19173011

  6. VCSA 6.5 升级 VCSA 6.7

    VCSA 6.7已于4月17日正式发布,文件名为VMware-VCSA-all-6.7.0-8217866.iso,国内百度网盘已有下载链接,请自行搜索. 下载后解压,运行\VMware-VCSA-a ...

  7. 黑盒测试用例设计——PICT(QQ实践)

     以QQ的状态设置来做一次实践.QQ用户可以对如下的状态方面的设置.后两张图是登录后的状态的可选项和鼠标键盘无动作后将状态切换至的可选项.默认的自动回复有三种,默认的快捷回复有四种.对于自动回复和快捷 ...

  8. 使用JSP页面生成PDF报表

    转自:http://developer.51cto.com/art/200907/134261.htm 1.iText简介 iText是一个开放源码的Java类库,可以用来方便地生成PDF文件.大家通 ...

  9. delphi android路径 TPath 文件路径,文件管理

    获取Android相关文档路径 delphi 新路径.文件功能 IOUtils单元,文件路径,文件管理 http://docwiki.embarcadero.com/RADStudio/Berlin/ ...

  10. XE 创建 Active Form

    XE6: http://docwiki.embarcadero.com/RADStudio/XE6/en/Generating_an_Active_Form_Based_on_a_VCL_Form h ...