【题解】

  要求出树上两点间的距离,树上的边有边权,本来应该是个LCA。

  看他数据小,Xjb水过去了。。。其实也算是LCA吧,一个O(n)的LCA。。。

#include<cstdio>
#include<algorithm>
#define N (1010)
#define rg register
#define LL long long
using namespace std;
int n,m,root,cnt,tot,dep[N],in[N],out[N],fa[N],last[N],a[N][N];
LL ans=0;
struct edge{
int to,pre,dis;
}e[N<<1];
inline int read(){
int k=0,f=1; char c=getchar();
while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
return k*f;
}
inline void add(int x,int y,int z){e[++tot]=(edge){y,last[x],z}; last[x]=tot;}
void dfs(int x,int father){
in[x]=++cnt; dep[x]=dep[father]+1;
for(rg int i=last[x],to;i;i=e[i].pre) if((to=e[i].to)!=father) dfs(to,fa[to]=x);
out[x]=++cnt;
}
inline bool IN(int x,int y){
if(in[y]<=in[x]&&out[x]<=out[y]) return 1;
return 0;
}
int main(){
n=read(); m=read();
for(rg int i=1,u,v,dis;i<n;i++){
u=read(),v=read(),dis=read();
add(u,v,dis),add(v,u,dis);
a[u][v]=a[v][u]=dis;
}
dfs(1,0);
//for(int i=1;i<=n;i++) printf("%d %d\n",in[i],out[i]);
for(rg int i=1,x,y;i<=m;i++){
x=read(); y=read(); ans=0;
if(dep[x]<dep[y]) swap(x,y);
while(!IN(y,x)) ans+=a[x][fa[x]],x=fa[x];
while(x!=y) ans+=a[y][fa[y]],y=fa[y];
printf("%lld\n",ans);
}
return 0;
}

  

BZOJ 1602 USACO 2008 Oct. 牧场行走的更多相关文章

  1. BZOJ 1603 USACO 2008 Oct. 打谷机

    [题解] 水题.. 保存连接方式,按顺序处理即可. #include<cstdio> #include<algorithm> using namespace std; int ...

  2. BZOJ 1601 USACO 2008 Oct. 灌水

    [Description] Farmer John已经决定把水灌到他的n(1<=n<=300)块农田,农田被数字1到n标记.把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水 ...

  3. bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 379  Solved: 216[Submit][Sta ...

  4. BZOJ 1602: [Usaco2008 Oct]牧场行走( 最短路 )

    一棵树..或许用LCA比较好吧...但是我懒...写了个dijkstra也过了.. ---------------------------------------------------------- ...

  5. 1602: [Usaco2008 Oct]牧场行走

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1211  Solved: 616 [Submit][ ...

  6. BZOJ1602: [Usaco2008 Oct]牧场行走

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1084  Solved: 556[Submit][St ...

  7. 【bzoj1602】[Usaco2008 Oct]牧场行走

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1793  Solved: 935[Submit][St ...

  8. 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...

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

随机推荐

  1. JSP-Runood:JSP 客户端请求

    ylbtech-JSP-Runood:JSP 客户端请求 1.返回顶部 1. JSP 客户端请求 当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信息 ...

  2. Word中公式和文字混排对齐的问题

    全选-字体-字符间距-位置-标准-确定 段落-中文版式-文本对齐方式-居中-确定

  3. E20170619-hm

    bucket   n. 水桶; open hash  [词典] [计] 开放散列,开混列; spirit   n. 精神,心灵; 情绪; 勇气; 精髓; flesh   n. 肉; 肉体; 果肉; 皮 ...

  4. ajax 以json 的形式来传递返回参数的实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestWcf.aspx.c ...

  5. easyui DatagrId 的实例讲解

    下面是代码实现 @{    ViewBag.Title = "人员查找";    ViewBag.LeftWidth = "200px";    ViewBag ...

  6. c++小游戏

    #include <iostream> using namespace std; double shengmingli=2000;//定义主角初始生命力 int gongjili=150; ...

  7. 【知识总结】扩展卢卡斯定理(exLucas)

    扩展卢卡斯定理用于求如下式子(其中\(p\)不一定是质数): \[C_n^m\ mod\ p\] 我们将这个问题由总体到局部地分为三个层次解决. 层次一:原问题 首先对\(p\)进行质因数分解: \[ ...

  8. ACM_他和她(最大生成树+最短路径)

    他和她 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大二上学期刚过完,平时成绩不错的小V参加了一个小型编程比赛,遇到一道题,虽 ...

  9. 336 Palindrome Pairs 回文对

    给定一组独特的单词, 找出在给定列表中不同 的索引对(i, j),使得关联的两个单词,例如:words[i] + words[j]形成回文.示例 1:给定 words = ["bat&quo ...

  10. 元组Tuple、数组Array、映射Map

    一.元组Tuple 元组Tuple是不同类型的值的聚集,元组的值将单个的值包含在圆括号中来构成,元组可以包含一个不同类型的元素 如 val riple = (100, "Scala" ...