【题解】

  要求出树上两点间的距离,树上的边有边权,本来应该是个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. [Python] partial改变方法默认參数

    Python 标准库中 functools库中有非常多对方法非常有有操作的封装,partial Objects就是当中之中的一个,他是对方法參数默认值的改动. 以下就看下简单的应用測试. #!/usr ...

  2. oc82--成员变量使用copy修饰

    // // Person.h #import <Foundation/Foundation.h> typedef void (^myBlock)(); @interface Person ...

  3. Codeforces Round #142 (Div. 2)B. T-primes

    B. T-primes time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  4. 在word中doc与docx的区别是什么(整理)

    在word中doc与docx的区别是什么(整理) docx 是Office2007使用的,是用新的基于XML的压缩文件格式取代了其目前专有的默认文件格式,在传统的文件名扩展名后面添加了字母x(即.do ...

  5. C# 数据库访问

    C# 数据库访问 分类: C#学习笔记2011-07-05 11:26 515人阅读 评论(0) 收藏 举报 数据库c#datasettextboxcommandexception   目录(?)[+ ...

  6. 洛谷P2516 [HAOI2010]最长公共子序列

    题目描述 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X="x0,x1,-,xm-1",序列Y=& ...

  7. JSP-Runoob:JSP 日期处理

    ylbtech-JSP-Runoob:JSP 日期处理 1.返回顶部 1. JSP 日期处理 使用JSP最重要的优势之一,就是可以使用所有Java  API.本章将会详细地讲述Java中的Date类, ...

  8. 2.android

    ImageButton action_btn = (ImageButton) findViewById(R.id.action_btn);action_btn.setOnClickListener(n ...

  9. [转载]马士兵Java视频教程 —— 学习顺序

    书(Java核心编程)+视频..这样学感觉比较好.. 原文地址:马士兵Java视频教程 —— 学习顺序作者:习惯 第一部分:J2se学习视频内容包括: 尚学堂科技_马士兵_JAVA视频教程_JDK5. ...

  10. C# System.Environment.GetFolderPath的使用 [转]

    原文:https://blog.csdn.net/yongyong521/article/details/75105853 获取系统文件目录 string strPath = Environment. ...