快乐树剖

 #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. HTML|CSS之前端入门

    知识内容: 1.计算机网络综述 2.web基础 3.HTML与CSS介绍 4.JavaScript与jQuery介绍 一.计算机网络综述 1.什么是计算机网络 计算机网络是指将地理位置不同.具有独立功 ...

  2. php7---redis

    wget https://github.com/phpredis/phpredis/archive/php7.zip cd /usr/local/src/ phpredis-php7 /usr/loc ...

  3. jmap Exception in thread "main" java.io.IOException: 拒绝访问。

    环境: 现有一个独立运行的系统S(有独立的jre,但是没jdk),现想通过jmap导出其内存堆栈信息.于是另外安装一个jdk.可是jdk的版本跟S系统的jre不能对应上.出了很多错误. 总是报错: C ...

  4. rhel7配置ELK过程

    参考网站:https://www.cnblogs.com/hongdada/p/7887455.html https://my.oschina.net/codingcloud/blog/1615013 ...

  5. c# DbProviderFactories 多数据库支持工程模式

    DbProviderFactories.GetFactory(dbProviderName) DBProviderFactory factory = DBProviderFactorys.GetFac ...

  6. ant 注意

    nt文件在部署时,如果控制台出现乱码则需要调整语言. 高版本eclipse在jdk高版本中已经植入了ant的部署.因此不需要单独配置ant.jar. 如果版本低,可下载ant插件,或者下载ant的工具 ...

  7. Spring声明式事务不回滚问题

    疑问,确实像往常一样在service上添加了注解 @Transactional,为什么查询数据库时还是发现有数据不一致的情况,想想肯定是事务没起作用,出现异常的时候数据没有回滚.于是就对相关代码进行了 ...

  8. Java对字符串使用MD5进行加密(亲测有效)

    转自:https://blog.csdn.net/jay314159/article/details/4918358 前言: MD5即Message-Digest Algorithm 5(信息-摘要算 ...

  9. 下载gradle缓慢的解决方法

    用AndroidStudio或者Qt编译apk,下载gradle缓慢时,可以用迅雷等下载工具在https://services.gradle.org/distributions/下载对应的版本. 中断 ...

  10. Mysql 知识(2)

    1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一 ...