题目描述

The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures also conveniently numbered 1..N. Most conveniently of all, cow i is grazing in pasture i.

Some pairs of pastures are connected by one of N-1 bidirectional walkways that the cows can traverse. Walkway i connects pastures A_i and B_i (1 <= A_i <= N; 1 <= B_i <= N) and has a length of L_i (1 <= L_i <= 10,000).

The walkways are set up in such a way that between any two distinct pastures, there is exactly one path of walkways that travels between them. Thus, the walkways form a tree.

The cows are very social and wish to visit each other often. Ever in a hurry, they want you to help them schedule their visits by computing the lengths of the paths between 1 <= L_i <= 10,000 pairs of pastures (each pair given as a query p1,p2 (1 <= p1 <= N; 1 <= p2 <= N).

POINTS: 200

有N(2<=N<=1000)头奶牛,编号为1到W,它们正在同样编号为1到N的牧场上行走.为了方 便,我们假设编号为i的牛恰好在第i号牧场上.

有一些牧场间每两个牧场用一条双向道路相连,道路总共有N - 1条,奶牛可以在这些道路 上行走.第i条道路把第Ai个牧场和第Bi个牧场连了起来(1 <= A_i <= N; 1 <= B_i <= N),而它的长度 是 1 <= L_i <= 10,000.在任意两个牧场间,有且仅有一条由若干道路组成的路径相连.也就是说,所有的道路构成了一棵树.

奶牛们十分希望经常互相见面.它们十分着急,所以希望你帮助它们计划它们的行程,你只 需要计算出Q(1 < Q < 1000)对点之间的路径长度•每对点以一个询问p1,p2 (1 <= p1 <= N; 1 <= p2 <= N). 的形式给出.

输入输出格式

输入格式:

  • Line 1: Two space-separated integers: N and Q

  • Lines 2..N: Line i+1 contains three space-separated integers: A_i, B_i, and L_i

  • Lines N+1..N+Q: Each line contains two space-separated integers representing two distinct pastures between which the cows wish to travel: p1 and p2

输出格式:

  • Lines 1..Q: Line i contains the length of the path between the two pastures in query i.

输入输出样例

输入样例#1:

4 2
2 1 2
4 3 2
1 4 3
1 2
3 2
输出样例#1:

2
7

说明

Query 1: The walkway between pastures 1 and 2 has length 2.

Query 2: Travel through the walkway between pastures 3 and 4, then the one between 4 and 1, and finally the one between 1 and 2, for a total length of 7.

路径是一条树两点间的距离用lca求解

考虑边权问题,由于倍增法从节点1开始dfs处理深度与边权,可以处理出前缀和,那么两点间路径边权和就是dis[a]+dis[b]-2*dis[lca(a,b)]

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int n,m,num,head[maxn];
struct node{
int v,w,next;
}edge[maxn];int dad[maxn][],deep[maxn],dis[maxn];
inline void add_edge(int u,int v,int w) {
edge[++num].v=v,edge[num].w=w,edge[num].next=head[u],head[u]=num;
}
void dfs(int x) {
deep[x]=deep[dad[x][]]+;
for(int i=;dad[x][i];i++)
dad[x][i+]=dad[dad[x][i]][i];
for(int i=head[x];i;i=edge[i].next)
if(!deep[edge[i].v]) {
dis[edge[i].v]=dis[x]+edge[i].w;
//printf("%d - > %d : %d\n",x,edge[i].v,edge[i].v);
dad[edge[i].v][]=x;
dfs(edge[i].v);
}
}
int lca(int x,int y) {
if(deep[x]>deep[y])swap(x,y);
for(int i=;i>=;i--)
if(deep[dad[y][i]]>=deep[x])y=dad[y][i];
if(x==y)return x;
for(int i=;i>=;i--)
if(dad[x][i]!=dad[y][i]) {
x=dad[x][i];
y=dad[y][i];
}
return dad[x][];
}
int main () {
scanf("%d%d",&n,&m);
for(int a,b,c,i=;i<n;++i) {
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);add_edge(b,a,c);
}
dfs();
int a,b;
while(m--) {
scanf("%d%d",&a,&b);
printf("%d\n",dis[a]+dis[b]-*dis[lca(a,b)]);
//printf("%d\n",dis[4]);
}
return ;
}

luogu P2912 [USACO08OCT]牧场散步Pasture Walking的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  7. Luogu 2912 [USACO08OCT]牧场散步Pasture Walking

    快乐树剖 #include<cstdio> #include<cstring> #include<algorithm> #define rd read() #def ...

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

    题目描述 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. sublime点击预览未起作用?教你如何设置支持浏览器预览

    我用的text3版,其他版本未试,但应该也有效. 安了个view in browser插件,然而点击预览未起作用. 搜解决方法,发现了另一个插件,sidebar enhancements,设置快捷键预 ...

  2. mysql:10道mysql查询语句面试题

    表结构 学生表student(id,name) 课程表course(id,name) 学生课程表student_course(sid,cid,score) 创建表的sql代码 ```sql creat ...

  3. 编译openwrt_MT7688_hiwooya

    参考链接: 无涯论坛地址: http://www.hi-wooya.com/forum.php openwrt官网地址:https://openwrt.org/zh-cn/doc/howto/buil ...

  4. php-7.0.16 , apache2.4.25 配置

    官网下载php,apache 修改apache E:\php\Apache24\conf\httpd.conf Define SRVROOT "E:/php/Apache24" - ...

  5. perl学习之:匹配修饰符/s /m

    m 是将字符串作为多行处理,s是将字符串作为单行处理,如果是s在字符串中出现的\n就相当于普通字符. 6.6. Matching Within Multiple Lines6.6.1. Problem ...

  6. Pandas中loc,iloc与直接切片的区别

    最近使用pandas,一直搞不清楚其中几种切片方法的区别,今天专门看了一下. 0. 把Series的行index或Dataframe的列名直接当做属性来索引. 如: s.index_name df.c ...

  7. pytorch 加载数据集

    pytorch初学者,想加载自己的数据,了解了一下数据类型.维度等信息,方便以后加载其他数据. 1 torchvision.transforms实现数据预处理 transforms.Totensor( ...

  8. Python9-模块2-序列化-day20

    序列化 什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 序列就是字符串 序列化的目的1.以某种存储形式使自定义对象持久化:2.将对象从一个地方传递到另一个地方.3.使程序 ...

  9. PYDay14:反射、面向对象基础-封装、集成、多态

    1.反射 通过字符串的形式,导入模块再通过字符串的形式,去模块中寻找指定的函数并执行eg:__import__(模块) 更加字符串的形式去对象(某个模块)中操作其成员 常用方法: getattr() ...

  10. luogu3755 [CQOI2017]老C的任务

    扫描线水题. #include <algorithm> #include <iostream> #include <cstdio> using namespace ...