[luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)
水题。
直接倍增求lca。
x到y的距离为dis[x] + dis[y] - 2 * dis[lca(x, y)]
——代码
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAXN 20002 using namespace std; int n, q, cnt;
int head[MAXN], to[MAXN], next[MAXN], val[MAXN], deep[MAXN], f[MAXN][], dis[MAXN]; inline void add(int x, int y, int z)
{
to[cnt] = y;
val[cnt] = z;
next[cnt] = head[x];
head[x] = cnt++;
} inline void dfs(int u)
{
int i, v;
deep[u] = deep[f[u][]] + ;
for(i = ; f[u][i]; i++) f[u][i + ] = f[f[u][i]][i];
for(i = head[u]; i != -; i = next[i])
{
v = to[i];
if(!deep[v])
{
f[v][] = u;
dis[v] = dis[u] + val[i];
dfs(v);
}
}
} inline int lca(int x, int y)
{
int i;
if(deep[x] < deep[y]) swap(x, y);
for(i = ; i >= ; i--)
if(deep[f[x][i]] >= deep[y])
x = f[x][i];
if(x == y) return x;
for(i = ; i >= ; i--)
if(f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][];
} int main()
{
int i, x, y, z;
scanf("%d %d", &n, &q);
memset(head, -, sizeof(head));
for(i = ; i < n; i++)
{
scanf("%d %d %d", &x, &y, &z);
add(x, y, z);
add(y, x, z);
}
deep[] = ;
dfs();
for(i = ; i <= q; i++)
{
scanf("%d %d", &x, &y);
printf("%d\n", dis[x] + dis[y] - * dis[lca(x, y)]);
}
return ;
}
[luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)的更多相关文章
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- 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 ...
- [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 ...
- luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 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 ...
- 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 ...
- Luogu 2912 [USACO08OCT]牧场散步Pasture Walking
快乐树剖 #include<cstdio> #include<cstring> #include<algorithm> #define rd read() #def ...
随机推荐
- Java实现读取文章中重复出现的中文字符串
在上个星期阿里巴巴一面的时候,最后面试官问我如何把一篇文章中重复出现的词或者句子找出来,当时太紧张,答的不是很好.今天有时间再来亲手实现一遍.其实说白了也就是字符串的处理,所以难度并不是很大. 以下是 ...
- WP8.1小梦词典开发2:百度翻译API使用
原文出自:http://www.bcmeng.com/api2/ 小梦昨天和大家分享了WP8.1金山词霸API使用方法,今天继续分享windows phone 8.1中百度翻译API的使用方法.和昨天 ...
- UI自动化测试表单重要代码
public class frame { public static void main(String[] args) { // TODO Auto-generated method stub Sys ...
- iOS开发之控制器创建与加载(生命周期)
1.如何创建一个控制器 控制器常见的创建方式有以下几种: (1)通过storyboard创建 (2)直接创建 MJViewController *mj = [[MJViewController all ...
- Python快速入门(4)
输入输出: open() read() readine() readlines() write() pickle模块可以做序列化操作,持久保持对象的信息. 我们可以很容易的读写文件中的字符串.数值就要 ...
- JSON与JAVA的数据转换
http://developer.51cto.com/art/200906/129090.htm java.lang.ClassNotFoundException: net.sf.json.JSONA ...
- webots自学笔记(六)实用控制器函数补充
原创文章,来自"博客园,_阿龙clliu" http://www.cnblogs.com/clliu/,转载请注明原文章出处. 用Webots软件做机器人仿真时,可以编 ...
- JS——函数、事件
1.函数字符串函数 var s=new string(); var ss="hello world"; var sss=""HELLO, WORLD" ...
- 【Hexo】Hexo+Github构建个人博客 (三):添加皮肤主题
一.选择主题 选择你自己喜欢的主题 参考: 1.Hexo官网主题 2.知乎:有哪些好看的 Hexo 主题? 二.配置主题 1.我选择了hexo-theme-yilia这个主题,简洁大方,功能齐全: 这 ...
- android开发之-软件设置保存-快速学会使用SharedPreferences篇-实测
我们在设计软件的时候,需要记录软件设置的基本信息,那么怎么来保存他们呢?我们可以使用SharedPreferences. SharedPreferences是一个xml文件,用来存储软件的常规设置 ...