【题解】

  要求出树上两点间的距离,树上的边有边权,本来应该是个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. 使用WCF进行跨平台开发之一(WCF的实现、控制台托管与.net平台的调用)

    WCF是Windows Communication Foundation的缩写,是微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,是WinFx的三个重要开发类库之一,其它两个是WP ...

  2. MySQL之自定义函数实例讲解

    转自:https://www.2cto.com/database/201804/740205.html MySQL中已经有很多函数,如时间函数等,但是有时这些函数不能满足自己的设计需求,此时需要自定义 ...

  3. Gym - 100162G 2012-2013 Petrozavodsk Winter Training Camp G. Lyndon Words 暴力枚举

    题面 题意:如果一个字符串的最小表示法是他自己,他就是一个Lyndon Word. 例如  aabcb 他的循环串有 abcba  bcbaa cbaab baabc 其中字典序最小的是他自己 现在给 ...

  4. [Swift通天遁地]九、拔剑吧-(6)使用开源类库快速搭建强大的侧边栏项目

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. IOC框架---什么是IOC

    1 IoC理论的背景    我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑.                    ...

  6. 如何看待B站疑似源码泄漏的问题?

    今天突然看到关于B站源码泄漏事.网曝B站整个网站后台工程源码遭泄露,开源项目平台Github上疑似出现了Bilibili网站后台工程,内含部分用户名密码.目前官方还没对此事作出任何回应,所以还无法确定 ...

  7. linux 怎么在后台添加运行脚本,即使关机也可以用

    nohup ma.php >guangxindai.log 2>&1 & 或者 nohup ma.php & 在shell中,文件描述符通常是:STDIN标准输入, ...

  8. sql数据库中常用连接

    很简单的知识点,今天有点搞不清楚左外连接,右外连接:详见以下: --表stu id name 1, Jack 2, Tom 3, Kity 4, nono --表exam id grade 1, 56 ...

  9. SVD 学习笔记

    本文主要学习了这篇博客:http://www.cnblogs.com/LeftNotEasy/archive/2011/01/19/svd-and-applications.html,将SVD讲的恨透 ...

  10. “国家队爷”杯液体战争AI比赛!!__SymenYang

    原帖 这两天一直在搞这个AI,提供的样例更本不是我的风格啊,看不懂更不会改... 所以我自己写了一个AI的平台,现在在不断的修改AI的策略,smart样例还是很容易过的,让line的行走速度变慢一点到 ...